blob: 70800444e181b5bda573e4c5595b86736d6fcec4 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 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
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
8
[email protected]ad5d1422012-10-19 13:40:299#include "base/basictypes.h"
[email protected]4a23c374c2012-12-08 08:38:5510#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1411#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3412#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4813#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0914#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4015#include "cc/animation/timing_function.h"
[email protected]681ccff2013-03-18 06:13:5216#include "cc/base/math_util.h"
17#include "cc/base/util.h"
[email protected]6e84de22013-03-18 06:54:2718#include "cc/debug/debug_rect_history.h"
19#include "cc/debug/frame_rate_counter.h"
20#include "cc/debug/overdraw_metrics.h"
21#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4322#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2223#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2124#include "cc/input/page_scale_animation.h"
25#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5226#include "cc/layers/append_quads_data.h"
27#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2828#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5229#include "cc/layers/layer_iterator.h"
[email protected]50761e92013-03-29 20:51:2830#include "cc/layers/render_surface_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/scrollbar_layer_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3032#include "cc/output/compositor_frame_metadata.h"
33#include "cc/output/delegating_renderer.h"
34#include "cc/output/gl_renderer.h"
35#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5636#include "cc/quads/render_pass_draw_quad.h"
37#include "cc/quads/shared_quad_state.h"
38#include "cc/quads/solid_color_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4039#include "cc/resources/memory_history.h"
40#include "cc/resources/picture_layer_tiling.h"
41#include "cc/resources/prioritized_resource_manager.h"
[email protected]be4655a2013-03-18 08:36:3142#include "cc/scheduler/delay_based_time_source.h"
43#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0444#include "cc/trees/damage_tracker.h"
45#include "cc/trees/layer_tree_host.h"
46#include "cc/trees/layer_tree_host_common.h"
47#include "cc/trees/layer_tree_impl.h"
48#include "cc/trees/quad_culler.h"
49#include "cc/trees/single_thread_proxy.h"
50#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0651#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1352#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1453
[email protected]94f206c12012-08-25 00:09:1454namespace {
55
[email protected]c1bb5af2013-03-13 19:06:2756void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
57 if (visible) {
58 TRACE_EVENT_ASYNC_BEGIN1("webkit",
59 "LayerTreeHostImpl::SetVisible",
60 id,
61 "LayerTreeHostImpl",
62 id);
63 return;
64 }
[email protected]94f206c12012-08-25 00:09:1465
[email protected]c1bb5af2013-03-13 19:06:2766 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1467}
68
[email protected]c1bb5af2013-03-13 19:06:2769} // namespace
[email protected]94f206c12012-08-25 00:09:1470
[email protected]9c88e562012-09-14 22:21:3071namespace cc {
[email protected]94f206c12012-08-25 00:09:1472
[email protected]96baf3e2012-10-22 23:09:5573class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:2774 public:
75 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
76 LayerTreeHostImpl* layer_tree_host_impl,
77 scoped_refptr<DelayBasedTimeSource> time_source) {
78 return make_scoped_ptr(
79 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
80 time_source));
81 }
82 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:2683 time_source_->SetClient(NULL);
84 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:2785 }
86
[email protected]6d0e69d2013-03-20 14:53:2687 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:2788 // In single threaded mode we attempt to simulate changing the current
89 // thread by maintaining a fake thread id. When we switch from one
90 // thread to another, we construct DebugScopedSetXXXThread objects that
91 // update the thread id. This lets DCHECKS that ensure we're on the
92 // right thread to work correctly in single threaded mode. The problem
93 // here is that the timer tasks are run via the message loop, and when
94 // they run, we've had no chance to construct a DebugScopedSetXXXThread
95 // object. The result is that we report that we're running on the main
96 // thread. In multi-threaded mode, this timer is run on the compositor
97 // thread, so to keep this consistent in single-threaded mode, we'll
98 // construct a DebugScopedSetImplThread object. There is no need to do
99 // this in multi-threaded mode since the real thread id's will be
100 // correct. In fact, setting fake thread id's interferes with the real
101 // thread id's and causes breakage.
102 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
103 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
104 set_impl_thread.reset(
105 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14106 }
107
[email protected]c1bb5af2013-03-13 19:06:27108 layer_tree_host_impl_->ActivatePendingTreeIfNeeded();
[email protected]fb7425a2013-04-22 16:28:55109 layer_tree_host_impl_->Animate(
110 layer_tree_host_impl_->CurrentFrameTimeTicks(),
111 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18112 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
113 bool start_ready_animations = true;
114 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38115 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27116 }
[email protected]373974232013-01-10 22:20:50117
[email protected]c1bb5af2013-03-13 19:06:27118 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26119 if (active != time_source_->Active())
120 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27121 }
[email protected]94f206c12012-08-25 00:09:14122
[email protected]c1bb5af2013-03-13 19:06:27123 private:
124 LayerTreeHostImplTimeSourceAdapter(
125 LayerTreeHostImpl* layer_tree_host_impl,
126 scoped_refptr<DelayBasedTimeSource> time_source)
127 : layer_tree_host_impl_(layer_tree_host_impl),
128 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26129 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27130 }
[email protected]94f206c12012-08-25 00:09:14131
[email protected]c1bb5af2013-03-13 19:06:27132 LayerTreeHostImpl* layer_tree_host_impl_;
133 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14134
[email protected]c1bb5af2013-03-13 19:06:27135 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14136};
137
[email protected]96baf3e2012-10-22 23:09:55138LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20139 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27140
141LayerTreeHostImpl::FrameData::~FrameData() {}
142
143scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
144 const LayerTreeSettings& settings,
145 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43146 Proxy* proxy,
147 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
148 return make_scoped_ptr(
149 new LayerTreeHostImpl(settings,
150 client,
151 proxy,
152 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10153}
154
[email protected]372bad5f2013-03-21 16:38:43155LayerTreeHostImpl::LayerTreeHostImpl(
156 const LayerTreeSettings& settings,
157 LayerTreeHostImplClient* client,
158 Proxy* proxy,
159 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27160 : client_(client),
161 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37162 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27163 did_lock_scrolling_layer_(false),
164 should_bubble_scrolls_(false),
165 wheel_scrolling_(false),
[email protected]a23451e2013-06-07 20:58:26166 manage_tiles_needed_(false),
[email protected]1960a712013-04-30 17:06:47167 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27168 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27169 visible_(true),
170 managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34171 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27172 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
173 0,
174 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING),
175 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36176 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29177 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27178 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02179 debug_rect_history_(DebugRectHistory::Create()),
[email protected]d7626ffd2013-03-29 00:17:42180 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27181 last_sent_memory_visible_bytes_(0),
182 last_sent_memory_visible_and_nearby_bytes_(0),
183 last_sent_memory_use_bytes_(0),
[email protected]f224cc92013-06-06 23:23:32184 device_scale_factor_(1.f),
185 overdraw_bottom_height_(0.f),
[email protected]372bad5f2013-03-21 16:38:43186 animation_registrar_(AnimationRegistrar::Create()),
187 rendering_stats_instrumentation_(rendering_stats_instrumentation) {
[email protected]c1bb5af2013-03-13 19:06:27188 DCHECK(proxy_->IsImplThread());
189 DidVisibilityChange(this, visible_);
190
[email protected]8e0176d2013-03-21 03:14:52191 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27192
[email protected]8e0176d2013-03-21 03:14:52193 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27194 top_controls_manager_ =
195 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52196 settings.top_controls_height,
197 settings.top_controls_show_threshold,
198 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27199 }
200
[email protected]8e0176d2013-03-21 03:14:52201 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27202
203 // LTHI always has an active tree.
204 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51205 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
206 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10207}
208
[email protected]c1bb5af2013-03-13 19:06:27209LayerTreeHostImpl::~LayerTreeHostImpl() {
210 DCHECK(proxy_->IsImplThread());
211 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51212 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
213 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27214
[email protected]200a9c062013-05-20 04:34:37215 if (input_handler_client_) {
216 input_handler_client_->WillShutdown();
217 input_handler_client_ = NULL;
218 }
219
[email protected]c1bb5af2013-03-13 19:06:27220 if (active_tree_->root_layer()) {
221 ClearRenderSurfaces();
222 // The layer trees must be destroyed before the layer tree host. We've
223 // made a contract with our animation controllers that the registrar
224 // will outlive them, and we must make good.
225 recycle_tree_.reset();
226 pending_tree_.reset();
227 active_tree_.reset();
228 }
[email protected]94f206c12012-08-25 00:09:14229}
230
[email protected]c1bb5af2013-03-13 19:06:27231void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29232
[email protected]c1bb5af2013-03-13 19:06:27233void LayerTreeHostImpl::CommitComplete() {
234 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08235
[email protected]c1bb5af2013-03-13 19:06:27236 // Impl-side painting needs an update immediately post-commit to have the
237 // opportunity to create tilings. Other paths can call UpdateDrawProperties
238 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52239 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27240 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04241 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26242 // Start working on newly created tiles immediately if needed.
243 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27244 } else {
245 active_tree_->set_needs_update_draw_properties();
246 }
[email protected]3ba4cae2013-01-16 03:58:38247
[email protected]c1bb5af2013-03-13 19:06:27248 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14249}
250
[email protected]c1bb5af2013-03-13 19:06:27251bool LayerTreeHostImpl::CanDraw() {
252 // Note: If you are changing this function or any other function that might
253 // affect the result of CanDraw, make sure to call
254 // client_->OnCanDrawStateChanged in the proper places and update the
255 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14256
[email protected]c1bb5af2013-03-13 19:06:27257 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42258 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
259 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22260 return false;
[email protected]c1bb5af2013-03-13 19:06:27261 }
262 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42263 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
264 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27265 return false;
266 }
267 if (active_tree_->ViewportSizeInvalid()) {
268 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42269 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
270 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27271 return false;
272 }
273 if (!renderer_) {
[email protected]c76faea2013-03-26 07:42:42274 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
275 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27276 return false;
277 }
278 if (active_tree_->ContentsTexturesPurged()) {
279 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42280 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
281 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27282 return false;
283 }
284 return true;
[email protected]2f1acc262012-11-16 21:42:22285}
286
[email protected]c1bb5af2013-03-13 19:06:27287void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
288 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37289 if (input_handler_client_)
290 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27291 AnimatePageScale(monotonic_time);
292 AnimateLayers(monotonic_time, wall_clock_time);
293 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37294 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14295}
296
[email protected]c1bb5af2013-03-13 19:06:27297void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26298 if (!tile_manager_)
299 return;
300 if (!manage_tiles_needed_)
301 return;
302 manage_tiles_needed_ = false;
[email protected]c1bb5af2013-03-13 19:06:27303 tile_manager_->ManageTiles();
304
305 size_t memory_required_bytes;
306 size_t memory_nice_to_have_bytes;
307 size_t memory_used_bytes;
308 tile_manager_->GetMemoryStats(&memory_required_bytes,
309 &memory_nice_to_have_bytes,
310 &memory_used_bytes);
311 SendManagedMemoryStats(memory_required_bytes,
312 memory_nice_to_have_bytes,
313 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15314}
315
[email protected]c1bb5af2013-03-13 19:06:27316void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
317 bool anchor_point,
318 float page_scale,
319 base::TimeTicks start_time,
320 base::TimeDelta duration) {
321 if (!RootScrollLayer())
322 return;
323
324 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47325 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27326 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51327 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27328
329 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40330
331 // Easing constants experimentally determined.
332 scoped_ptr<TimingFunction> timing_function =
333 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
334
[email protected]c1bb5af2013-03-13 19:06:27335 page_scale_animation_ =
336 PageScaleAnimation::Create(scroll_total,
337 active_tree_->total_page_scale_factor(),
338 viewport_size,
339 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40340 start_time_seconds,
341 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27342
343 if (anchor_point) {
344 gfx::Vector2dF anchor(target_offset);
345 page_scale_animation_->ZoomWithAnchor(anchor,
346 page_scale,
347 duration.InSecondsF());
348 } else {
349 gfx::Vector2dF scaled_target_offset = target_offset;
350 page_scale_animation_->ZoomTo(scaled_target_offset,
351 page_scale,
352 duration.InSecondsF());
353 }
354
355 client_->SetNeedsRedrawOnImplThread();
356 client_->SetNeedsCommitOnImplThread();
357 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15358}
359
[email protected]c1bb5af2013-03-13 19:06:27360void LayerTreeHostImpl::ScheduleAnimation() {
361 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15362}
363
[email protected]c1bb5af2013-03-13 19:06:27364bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
365 if (!EnsureRenderSurfaceLayerList())
366 return false;
[email protected]f57bbc02012-11-21 07:02:15367
[email protected]c1bb5af2013-03-13 19:06:27368 gfx::PointF device_viewport_point =
369 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15370
[email protected]c1bb5af2013-03-13 19:06:27371 // First find out which layer was hit from the saved list of visible layers
372 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39373 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27374 device_viewport_point,
375 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15376
[email protected]c1bb5af2013-03-13 19:06:27377 // Walk up the hierarchy and look for a layer with a touch event handler
378 // region that the given point hits.
379 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39380 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27381 layer_impl))
382 return true;
383 }
[email protected]f57bbc02012-11-21 07:02:15384
[email protected]c1bb5af2013-03-13 19:06:27385 return false;
386}
387
388void LayerTreeHostImpl::TrackDamageForAllSurfaces(
389 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28390 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27391 // For now, we use damage tracking to compute a global scissor. To do this, we
392 // must compute all damage tracking before drawing anything, so that we know
393 // the root damage rect. The root damage rect is then used to scissor each
394 // surface.
395
396 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06397 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27398 --surface_index) {
399 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
400 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
401 DCHECK(render_surface);
402 render_surface->damage_tracker()->UpdateDamageTrackingState(
403 render_surface->layer_list(),
404 render_surface_layer->id(),
405 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
406 render_surface->content_rect(),
407 render_surface_layer->mask_layer(),
408 render_surface_layer->filters(),
409 render_surface_layer->filter().get());
410 }
411}
412
413void LayerTreeHostImpl::FrameData::AppendRenderPass(
414 scoped_ptr<RenderPass> render_pass) {
415 render_passes_by_id[render_pass->id] = render_pass.get();
416 render_passes.push_back(render_pass.Pass());
417}
418
[email protected]ffbb2212013-06-02 23:47:59419static DrawMode GetDrawMode(OutputSurface* output_surface) {
420 if (output_surface->ForcedDrawToSoftwareDevice()) {
421 return DRAW_MODE_RESOURCELESS_SOFTWARE;
422 } else if (output_surface->context3d()) {
423 return DRAW_MODE_HARDWARE;
424 } else {
425 DCHECK(output_surface->software_device());
426 return DRAW_MODE_SOFTWARE;
427 }
428}
429
[email protected]c1bb5af2013-03-13 19:06:27430static void AppendQuadsForLayer(RenderPass* target_render_pass,
431 LayerImpl* layer,
432 const OcclusionTrackerImpl& occlusion_tracker,
433 AppendQuadsData* append_quads_data) {
434 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49435 QuadCuller quad_culler(&target_render_pass->quad_list,
436 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27437 layer,
438 occlusion_tracker,
439 layer->ShowDebugBorders(),
440 for_surface);
441 layer->AppendQuads(&quad_culler, append_quads_data);
442}
443
444static void AppendQuadsForRenderSurfaceLayer(
445 RenderPass* target_render_pass,
446 LayerImpl* layer,
447 const RenderPass* contributing_render_pass,
448 const OcclusionTrackerImpl& occlusion_tracker,
449 AppendQuadsData* append_quads_data) {
450 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49451 QuadCuller quad_culler(&target_render_pass->quad_list,
452 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27453 layer,
454 occlusion_tracker,
455 layer->ShowDebugBorders(),
456 for_surface);
457
458 bool is_replica = false;
459 layer->render_surface()->AppendQuads(&quad_culler,
460 append_quads_data,
461 is_replica,
462 contributing_render_pass->id);
463
464 // Add replica after the surface so that it appears below the surface.
465 if (layer->has_replica()) {
466 is_replica = true;
467 layer->render_surface()->AppendQuads(&quad_culler,
468 append_quads_data,
469 is_replica,
470 contributing_render_pass->id);
471 }
472}
473
474static void AppendQuadsToFillScreen(
475 RenderPass* target_render_pass,
476 LayerImpl* root_layer,
477 SkColor screen_background_color,
478 const OcclusionTrackerImpl& occlusion_tracker) {
479 if (!root_layer || !SkColorGetA(screen_background_color))
480 return;
481
482 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
483 if (fill_region.IsEmpty())
484 return;
485
486 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49487 QuadCuller quad_culler(&target_render_pass->quad_list,
488 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27489 root_layer,
490 occlusion_tracker,
491 root_layer->ShowDebugBorders(),
492 for_surface);
493
494 // Manually create the quad state for the gutter quads, as the root layer
495 // doesn't have any bounds and so can't generate this itself.
496 // TODO(danakj): Make the gutter quads generated by the solid color layer
497 // (make it smarter about generating quads to fill unoccluded areas).
498
499 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
500 float opacity = 1.f;
501 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49502 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27503 shared_quad_state->SetAll(root_layer->draw_transform(),
504 root_target_rect.size(),
505 root_target_rect,
506 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01507 false,
[email protected]f57bbc02012-11-21 07:02:15508 opacity);
509
[email protected]c1bb5af2013-03-13 19:06:27510 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17511
[email protected]c1bb5af2013-03-13 19:06:27512 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
513 bool did_invert = root_layer->screen_space_transform().GetInverse(
514 &transform_to_layer_space);
515 DCHECK(did_invert);
516 for (Region::Iterator fill_rects(fill_region);
517 fill_rects.has_rect();
518 fill_rects.next()) {
519 // The root layer transform is composed of translations and scales only,
520 // no perspective, so mapping is sufficient (as opposed to projecting).
521 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21522 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27523 // Skip the quad culler and just append the quads directly to avoid
524 // occlusion checks.
525 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]10a30b12013-05-02 16:42:11526 quad->SetNew(shared_quad_state, layer_rect, screen_background_color, false);
[email protected]c7e95b42013-03-18 01:13:49527 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27528 }
[email protected]467b3612012-08-28 07:41:16529}
530
[email protected]c1bb5af2013-03-13 19:06:27531bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
532 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14533
[email protected]c1bb5af2013-03-13 19:06:27534 if (!CanDraw() || !active_tree_->root_layer())
535 return false;
[email protected]2d692992012-12-19 01:19:32536
[email protected]c1bb5af2013-03-13 19:06:27537 TrackDamageForAllSurfaces(active_tree_->root_layer(),
538 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14539
[email protected]e0341352013-04-06 05:01:20540 // If the root render surface has no visible damage, then don't generate a
541 // frame at all.
542 RenderSurfaceImpl* root_surface =
543 active_tree_->root_layer()->render_surface();
544 bool root_surface_has_no_visible_damage =
545 !root_surface->damage_tracker()->current_damage_rect().Intersects(
546 root_surface->content_rect());
547 bool root_surface_has_contributing_layers =
548 !root_surface->layer_list().empty();
549 if (root_surface_has_contributing_layers &&
550 root_surface_has_no_visible_damage) {
551 TRACE_EVENT0("cc",
552 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
553 frame->has_no_damage = true;
554 return true;
555 }
556
[email protected]c1bb5af2013-03-13 19:06:27557 TRACE_EVENT1("cc",
558 "LayerTreeHostImpl::CalculateRenderPasses",
559 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06560 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14561
[email protected]c1bb5af2013-03-13 19:06:27562 // Create the render passes in dependency order.
563 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06564 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27565 --surface_index) {
566 LayerImpl* render_surface_layer =
567 (*frame->render_surface_layer_list)[surface_index];
568 render_surface_layer->render_surface()->AppendRenderPasses(frame);
569 }
[email protected]94f206c12012-08-25 00:09:14570
[email protected]c1bb5af2013-03-13 19:06:27571 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52572 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27573 base::debug::TraceLog::GetInstance() &&
574 base::debug::TraceLog::GetInstance()->IsEnabled();
575 OcclusionTrackerImpl occlusion_tracker(
576 active_tree_->root_layer()->render_surface()->content_rect(),
577 record_metrics_for_frame);
578 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52579 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14580
[email protected]846f455b2013-03-18 19:07:41581 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27582 occlusion_tracker.set_occluding_screen_space_rects_container(
583 &frame->occluding_screen_space_rects);
584 }
[email protected]846f455b2013-03-18 19:07:41585 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27586 occlusion_tracker.set_non_occluding_screen_space_rects_container(
587 &frame->non_occluding_screen_space_rects);
588 }
[email protected]94f206c12012-08-25 00:09:14589
[email protected]c1bb5af2013-03-13 19:06:27590 // Add quads to the Render passes in FrontToBack order to allow for testing
591 // occlusion and performing culling during the tree walk.
592 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28593 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27594 RenderSurfaceImpl,
595 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14596
[email protected]c1bb5af2013-03-13 19:06:27597 // Typically when we are missing a texture and use a checkerboard quad, we
598 // still draw the frame. However when the layer being checkerboarded is moving
599 // due to an impl-animation, we drop the frame to avoid flashing due to the
600 // texture suddenly appearing in the future.
601 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25602 // When we have a copy request for a layer, we need to draw no matter
603 // what, as the layer may disappear after this frame.
604 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14605
[email protected]372bad5f2013-03-21 16:38:43606 int layers_drawn = 0;
607
[email protected]ffbb2212013-06-02 23:47:59608 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
609
[email protected]c1bb5af2013-03-13 19:06:27610 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09611 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27612 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09613 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27614 it != end;
615 ++it) {
616 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09617 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27618 RenderPass* target_render_pass =
619 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14620
[email protected]0e5f7142013-05-24 06:45:36621 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23622 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14623
[email protected]c1bb5af2013-03-13 19:06:27624 AppendQuadsData append_quads_data(target_render_pass->id);
[email protected]89228202012-08-29 03:20:30625
[email protected]18a70192013-04-26 16:18:25626 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36627 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25628 have_copy_request = true;
[email protected]0e5f7142013-05-24 06:45:36629 it->TakeCopyRequests(&target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25630 }
631 } else if (it.represents_contributing_render_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27632 RenderPass::Id contributing_render_pass_id =
633 it->render_surface()->RenderPassId();
634 RenderPass* contributing_render_pass =
635 frame->render_passes_by_id[contributing_render_pass_id];
636 AppendQuadsForRenderSurfaceLayer(target_render_pass,
637 *it,
638 contributing_render_pass,
639 occlusion_tracker,
640 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09641 } else if (it.represents_itself() &&
642 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27643 bool has_occlusion_from_outside_target_surface;
644 bool impl_draw_transform_is_unknown = false;
645 if (occlusion_tracker.Occluded(
646 it->render_target(),
647 it->visible_content_rect(),
648 it->draw_transform(),
649 impl_draw_transform_is_unknown,
650 it->is_clipped(),
651 it->clip_rect(),
652 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09653 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27654 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59655 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27656 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59657
[email protected]c1bb5af2013-03-13 19:06:27658 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57659
[email protected]c1bb5af2013-03-13 19:06:27660 if (it->HasContributingDelegatedRenderPasses()) {
661 RenderPass::Id contributing_render_pass_id =
662 it->FirstContributingRenderPassId();
663 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
664 frame->render_passes_by_id.end()) {
665 RenderPass* render_pass =
666 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14667
[email protected]c1bb5af2013-03-13 19:06:27668 AppendQuadsData append_quads_data(render_pass->id);
669 AppendQuadsForLayer(render_pass,
670 *it,
671 occlusion_tracker,
672 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57673
[email protected]c1bb5af2013-03-13 19:06:27674 contributing_render_pass_id =
675 it->NextContributingRenderPassId(contributing_render_pass_id);
676 }
[email protected]94f206c12012-08-25 00:09:14677 }
678
[email protected]c1bb5af2013-03-13 19:06:27679 AppendQuadsForLayer(target_render_pass,
680 *it,
681 occlusion_tracker,
682 &append_quads_data);
683 }
[email protected]89228202012-08-29 03:20:30684
[email protected]372bad5f2013-03-21 16:38:43685 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14686 }
687
[email protected]e1e768f2013-03-26 08:48:09688 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27689 target_render_pass->has_occlusion_from_outside_target_surface = true;
690
[email protected]e1e768f2013-03-26 08:48:09691 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43692 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09693 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27694 bool layer_has_animating_transform =
695 it->screen_space_transform_is_animating() ||
696 it->draw_transform_is_animating();
697 if (layer_has_animating_transform)
698 draw_frame = false;
699 }
700
[email protected]e1e768f2013-03-26 08:48:09701 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27702 frame->contains_incomplete_tile = true;
703
704 occlusion_tracker.LeaveLayer(it);
705 }
706
[email protected]18a70192013-04-26 16:18:25707 if (have_copy_request)
708 draw_frame = true;
709
[email protected]372bad5f2013-03-21 16:38:43710 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
711
[email protected]1d993172012-10-18 18:15:04712#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27713 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
714 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
715 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
716 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
717 != frame->render_passes_by_id.end());
718 }
[email protected]94f206c12012-08-25 00:09:14719#endif
[email protected]c1bb5af2013-03-13 19:06:27720 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14721
[email protected]c1bb5af2013-03-13 19:06:27722 if (!active_tree_->has_transparent_background()) {
723 frame->render_passes.back()->has_transparent_background = false;
724 AppendQuadsToFillScreen(frame->render_passes.back(),
725 active_tree_->root_layer(),
726 active_tree_->background_color(),
727 occlusion_tracker);
728 }
[email protected]94f206c12012-08-25 00:09:14729
[email protected]c1bb5af2013-03-13 19:06:27730 if (draw_frame)
731 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25732 else
733 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14734
[email protected]c1bb5af2013-03-13 19:06:27735 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37736 if (!output_surface_->ForcedDrawToSoftwareDevice())
737 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]fbe89f72013-05-21 07:24:24738 if (renderer_) {
739 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
740 frame);
741 }
[email protected]94f206c12012-08-25 00:09:14742
[email protected]e0341352013-04-06 05:01:20743 // If we're making a frame to draw, it better have at least one render pass.
744 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27745 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14746}
747
[email protected]200a9c062013-05-20 04:34:37748void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
749 if (input_handler_client_)
750 input_handler_client_->MainThreadHasStoppedFlinging();
751}
752
[email protected]3d9f7432013-04-06 00:35:18753void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
754 bool should_background_tick) {
755 bool enabled = should_background_tick &&
756 !animation_registrar_->active_animation_controllers().empty();
757
[email protected]c1bb5af2013-03-13 19:06:27758 // Lazily create the time_source adapter so that we can vary the interval for
759 // testing.
760 if (!time_source_client_adapter_) {
761 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
762 this,
[email protected]6d0e69d2013-03-20 14:53:26763 DelayBasedTimeSource::Create(LowFrequencyAnimationInterval(),
[email protected]c1bb5af2013-03-13 19:06:27764 proxy_->CurrentThread()));
765 }
[email protected]94f206c12012-08-25 00:09:14766
[email protected]c1bb5af2013-03-13 19:06:27767 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14768}
769
[email protected]b9d4a362013-04-23 05:36:27770void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02771 viewport_damage_rect_.Union(damage_rect);
772}
773
[email protected]c1bb5af2013-03-13 19:06:27774static inline RenderPass* FindRenderPassById(
775 RenderPass::Id render_pass_id,
776 const LayerTreeHostImpl::FrameData& frame) {
777 RenderPassIdHashMap::const_iterator it =
778 frame.render_passes_by_id.find(render_pass_id);
779 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14780}
781
[email protected]c1bb5af2013-03-13 19:06:27782static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
783 LayerTreeHostImpl::FrameData* frame) {
784 RenderPass* remove_render_pass =
785 FindRenderPassById(remove_render_pass_id, *frame);
786 // The pass was already removed by another quad - probably the original, and
787 // we are the replica.
788 if (!remove_render_pass)
789 return;
790 RenderPassList& render_passes = frame->render_passes;
791 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
792 render_passes.end(),
793 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14794
[email protected]c1bb5af2013-03-13 19:06:27795 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14796
[email protected]c1bb5af2013-03-13 19:06:27797 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
798 frame->render_passes.erase(to_remove);
799 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14800
[email protected]c1bb5af2013-03-13 19:06:27801 // Now follow up for all RenderPass quads and remove their RenderPasses
802 // recursively.
803 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29804 QuadList::ConstBackToFrontIterator quad_list_iterator =
805 quad_list.BackToFrontBegin();
806 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27807 ++quad_list_iterator) {
808 DrawQuad* current_quad = (*quad_list_iterator);
809 if (current_quad->material != DrawQuad::RENDER_PASS)
810 continue;
[email protected]94f206c12012-08-25 00:09:14811
[email protected]c1bb5af2013-03-13 19:06:27812 RenderPass::Id next_remove_render_pass_id =
813 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
814 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
815 }
[email protected]94f206c12012-08-25 00:09:14816}
817
[email protected]c1bb5af2013-03-13 19:06:27818bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
819 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
820 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24821 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27822 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
823 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06824 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27825 if (quad_has_damage) {
826 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
827 return false;
828 } else if (!quad_has_cached_resource) {
829 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
830 return false;
831 }
832 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
833 return true;
[email protected]94f206c12012-08-25 00:09:14834}
835
[email protected]c1bb5af2013-03-13 19:06:27836bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
837 const RenderPassDrawQuad& quad, const FrameData& frame) const {
838 const RenderPass* render_pass =
839 FindRenderPassById(quad.render_pass_id, frame);
840 if (!render_pass)
841 return false;
[email protected]94f206c12012-08-25 00:09:14842
[email protected]c1bb5af2013-03-13 19:06:27843 // If any quad or RenderPass draws into this RenderPass, then keep it.
844 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29845 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
846 quad_list.BackToFrontBegin();
847 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27848 ++quad_list_iterator) {
849 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14850
[email protected]c1bb5af2013-03-13 19:06:27851 if (current_quad->material != DrawQuad::RENDER_PASS)
852 return false;
[email protected]94f206c12012-08-25 00:09:14853
[email protected]c1bb5af2013-03-13 19:06:27854 const RenderPass* contributing_pass = FindRenderPassById(
855 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
856 if (contributing_pass)
857 return false;
858 }
859 return true;
[email protected]94f206c12012-08-25 00:09:14860}
861
862// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27863template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
864 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
865 CullRenderPassesWithCachedTextures culler, FrameData* frame);
866template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
867 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
868 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14869
870// static
[email protected]c1bb5af2013-03-13 19:06:27871template <typename RenderPassCuller>
872void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
873 FrameData* frame) {
874 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
875 it != culler.RenderPassListEnd(frame->render_passes);
876 it = culler.RenderPassListNext(it)) {
877 const RenderPass* current_pass = frame->render_passes[it];
878 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29879 QuadList::ConstBackToFrontIterator quad_list_iterator =
880 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14881
[email protected]ed511b8d2013-03-25 03:29:29882 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27883 ++quad_list_iterator) {
884 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14885
[email protected]c1bb5af2013-03-13 19:06:27886 if (current_quad->material != DrawQuad::RENDER_PASS)
887 continue;
[email protected]94f206c12012-08-25 00:09:14888
[email protected]c1bb5af2013-03-13 19:06:27889 const RenderPassDrawQuad* render_pass_quad =
890 RenderPassDrawQuad::MaterialCast(current_quad);
891 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
892 continue;
[email protected]94f206c12012-08-25 00:09:14893
[email protected]c1bb5af2013-03-13 19:06:27894 // We are changing the vector in the middle of iteration. Because we
895 // delete render passes that draw into the current pass, we are
896 // guaranteed that any data from the iterator to the end will not
897 // change. So, capture the iterator position from the end of the
898 // list, and restore it after the change.
899 size_t position_from_end = frame->render_passes.size() - it;
900 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
901 it = frame->render_passes.size() - position_from_end;
902 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14903 }
[email protected]c1bb5af2013-03-13 19:06:27904 }
[email protected]94f206c12012-08-25 00:09:14905}
906
[email protected]e0341352013-04-06 05:01:20907bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
908 gfx::Rect device_viewport_damage_rect) {
[email protected]c1bb5af2013-03-13 19:06:27909 TRACE_EVENT0("cc", "LayerTreeHostImpl::PrepareToDraw");
[email protected]94f206c12012-08-25 00:09:14910
[email protected]7d19dc342013-05-02 22:02:04911 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:27912
[email protected]c1bb5af2013-03-13 19:06:27913 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
914 frame->render_passes.clear();
915 frame->render_passes_by_id.clear();
916 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:20917 frame->contains_incomplete_tile = false;
918 frame->has_no_damage = false;
919
920 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:02921 device_viewport_damage_rect.Union(viewport_damage_rect_);
922 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:20923
924 active_tree_->root_layer()->render_surface()->damage_tracker()->
925 AddDamageNextUpdate(device_viewport_damage_rect);
926 }
[email protected]94f206c12012-08-25 00:09:14927
[email protected]c1bb5af2013-03-13 19:06:27928 if (!CalculateRenderPasses(frame))
929 return false;
[email protected]94f206c12012-08-25 00:09:14930
[email protected]e4c3c87a2013-04-22 02:28:40931 frame->latency_info = active_tree_->GetLatencyInfo();
932
[email protected]c1bb5af2013-03-13 19:06:27933 // If we return true, then we expect DrawLayers() to be called before this
934 // function is called again.
935 return true;
[email protected]94f206c12012-08-25 00:09:14936}
937
[email protected]c1bb5af2013-03-13 19:06:27938void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
939 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:12940
[email protected]c1bb5af2013-03-13 19:06:27941 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:35942 visible_ ? policy.bytes_limit_when_visible
943 : policy.bytes_limit_when_not_visible,
944 ManagedMemoryPolicy::PriorityCutoffToValue(
945 visible_ ? policy.priority_cutoff_when_visible
946 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:27947 if (evicted_resources) {
948 active_tree_->SetContentsTexturesPurged();
949 if (pending_tree_)
950 pending_tree_->SetContentsTexturesPurged();
951 client_->SetNeedsCommitOnImplThread();
952 client_->OnCanDrawStateChanged(CanDraw());
953 client_->RenewTreePriority();
954 }
955 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:43956
[email protected]8be1a9bf2013-05-01 03:45:19957 UpdateTileManagerMemoryPolicy(policy);
958}
959
960void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
961 const ManagedMemoryPolicy& policy) {
962 if (!tile_manager_)
963 return;
964
965 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
966 new_state.memory_limit_in_bytes = visible_ ?
967 policy.bytes_limit_when_visible :
968 policy.bytes_limit_when_not_visible;
969 // TODO(reveman): We should avoid keeping around unused resources if
970 // possible. crbug.com/224475
971 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
972 (static_cast<int64>(new_state.memory_limit_in_bytes) *
973 settings_.max_unused_resource_memory_percentage) / 100);
974 new_state.memory_limit_policy =
975 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
976 visible_ ?
977 policy.priority_cutoff_when_visible :
978 policy.priority_cutoff_when_not_visible);
979 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:26980 manage_tiles_needed_ = true;
[email protected]94f206c12012-08-25 00:09:14981}
982
[email protected]c1bb5af2013-03-13 19:06:27983bool LayerTreeHostImpl::HasImplThread() const {
984 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:44985}
986
[email protected]86126792013-03-16 20:07:54987void LayerTreeHostImpl::DidInitializeVisibleTile() {
988 // TODO(reveman): Determine tiles that changed and only damage
989 // what's necessary.
990 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:27991 if (client_)
[email protected]86126792013-03-16 20:07:54992 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:47993}
994
[email protected]fcb846d2013-05-22 01:42:36995bool LayerTreeHostImpl::
996 ShouldForceTileUploadsRequiredForActivationToComplete() const {
997 // During shutdown of TileManager, it will attempt to flush its job queue,
998 // which can call this function while this is NULL.
999 if (!tile_manager_)
1000 return false;
1001
1002 TreePriority tree_priority = tile_manager_->GlobalState().tree_priority;
1003 return tree_priority != SMOOTHNESS_TAKES_PRIORITY &&
1004 animation_registrar_->active_animation_controllers().empty();
1005}
1006
[email protected]c1bb5af2013-03-13 19:06:271007bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:521008 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:521009}
1010
[email protected]c1bb5af2013-03-13 19:06:271011void LayerTreeHostImpl::SetManagedMemoryPolicy(
1012 const ManagedMemoryPolicy& policy) {
1013 if (managed_memory_policy_ == policy)
1014 return;
[email protected]61de5812012-11-08 07:03:441015
[email protected]d7626ffd2013-03-29 00:17:421016 // If there is already enough memory to draw everything imaginable and the
1017 // new memory limit does not change this, then do not re-commit. Don't bother
1018 // skipping commits if this is not visible (commits don't happen when not
1019 // visible, there will almost always be a commit when this becomes visible).
1020 bool needs_commit = true;
1021 if (visible() &&
1022 policy.bytes_limit_when_visible >=
1023 max_memory_needed_bytes_ &&
1024 managed_memory_policy_.bytes_limit_when_visible >=
1025 max_memory_needed_bytes_ &&
1026 policy.priority_cutoff_when_visible ==
1027 managed_memory_policy_.priority_cutoff_when_visible) {
1028 needs_commit = false;
1029 }
1030
[email protected]c1bb5af2013-03-13 19:06:271031 managed_memory_policy_ = policy;
1032 if (!proxy_->HasImplThread()) {
[email protected]d7626ffd2013-03-29 00:17:421033 // In single-thread mode, this can be called on the main thread by
1034 // GLRenderer::OnMemoryAllocationChanged.
[email protected]c1bb5af2013-03-13 19:06:271035 DebugScopedSetImplThread impl_thread(proxy_);
[email protected]206a3922013-05-17 06:34:121036 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271037 } else {
1038 DCHECK(proxy_->IsImplThread());
[email protected]206a3922013-05-17 06:34:121039 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271040 }
[email protected]d7626ffd2013-03-29 00:17:421041
1042 if (needs_commit)
1043 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141044}
1045
[email protected]f224cc92013-06-06 23:23:321046void LayerTreeHostImpl::SetExternalDrawConstraints(
1047 const gfx::Transform& transform,
1048 gfx::Rect viewport) {
1049 external_transform_ = transform;
1050 external_viewport_ = viewport;
1051}
1052
[email protected]1cd9f5552013-04-26 04:22:031053void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1054 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1055}
1056
[email protected]c1bb5af2013-03-13 19:06:271057void LayerTreeHostImpl::OnVSyncParametersChanged(base::TimeTicks timebase,
1058 base::TimeDelta interval) {
1059 client_->OnVSyncParametersChanged(timebase, interval);
[email protected]94f206c12012-08-25 00:09:141060}
1061
[email protected]c8cbae72013-05-23 10:45:031062void LayerTreeHostImpl::BeginFrame(base::TimeTicks frame_time) {
1063 client_->BeginFrameOnImplThread(frame_time);
[email protected]7ed47512013-03-26 22:28:451064}
1065
[email protected]c1bb5af2013-03-13 19:06:271066void LayerTreeHostImpl::OnSendFrameToParentCompositorAck(
1067 const CompositorFrameAck& ack) {
1068 if (!renderer_)
1069 return;
[email protected]b6f3d7e2012-12-08 00:11:211070
[email protected]c1bb5af2013-03-13 19:06:271071 // TODO(piman): We may need to do some validation on this ack before
1072 // processing it.
1073 renderer_->ReceiveCompositorFrameAck(ack);
[email protected]bb1e2822013-04-17 22:06:011074
1075 // When using compositor frame data, the ack doubles as a swap complete ack.
1076 OnSwapBuffersComplete();
[email protected]a46f32932012-12-07 21:43:161077}
1078
[email protected]c1bb5af2013-03-13 19:06:271079void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1080 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291081}
1082
[email protected]c1bb5af2013-03-13 19:06:271083CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1084 CompositorFrameMetadata metadata;
1085 metadata.device_scale_factor = device_scale_factor_;
1086 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1087 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1088 metadata.root_layer_size = active_tree_->ScrollableSize();
1089 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1090 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1091 if (top_controls_manager_) {
1092 metadata.location_bar_offset =
1093 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1094 metadata.location_bar_content_translation =
1095 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011096 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271097 }
[email protected]bf189f62012-12-18 03:42:111098
[email protected]c1bb5af2013-03-13 19:06:271099 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111100 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271101
[email protected]ffb2720f2013-03-15 19:18:371102 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]e4c3c87a2013-04-22 02:28:401103 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271104
1105 return metadata;
[email protected]bf189f62012-12-18 03:42:111106}
1107
[email protected]73673592013-04-03 22:14:321108bool LayerTreeHostImpl::AllowPartialSwap() const {
1109 // We don't track damage on the HUD layer (it interacts with damage tracking
1110 // visualizations), so disable partial swaps to make the HUD layer display
1111 // properly.
1112 return !debug_state_.ShowHudRects();
1113}
1114
[email protected]ff1211d2013-06-07 01:58:351115class DidBeginTracingFunctor {
1116 public:
1117 void operator()(LayerImpl* layer) {
1118 layer->DidBeginTracing();
1119 }
1120};
[email protected]37349bc2013-06-04 01:31:521121
[email protected]f0c2a242013-03-15 19:34:521122void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1123 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:271124 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
1125 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201126
1127 if (frame->has_no_damage)
1128 return;
1129
[email protected]c1bb5af2013-03-13 19:06:271130 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141131
[email protected]9e3594522013-03-18 00:57:361132 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:141133
[email protected]372bad5f2013-03-21 16:38:431134 rendering_stats_instrumentation_->SetScreenFrameCount(
1135 fps_counter_->current_frame_number());
1136 rendering_stats_instrumentation_->SetDroppedFrameCount(
1137 fps_counter_->dropped_frame_count());
1138
[email protected]c1bb5af2013-03-13 19:06:271139 if (tile_manager_) {
1140 memory_history_->SaveEntry(
1141 tile_manager_->memory_stats_from_last_assign());
1142 }
[email protected]1191d9d2013-02-02 06:00:331143
[email protected]846f455b2013-03-18 19:07:411144 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021145 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271146 active_tree_->root_layer(),
1147 *frame->render_surface_layer_list,
1148 frame->occluding_screen_space_rects,
1149 frame->non_occluding_screen_space_rects,
1150 debug_state_);
1151 }
[email protected]94f206c12012-08-25 00:09:141152
[email protected]a848c102013-03-26 08:59:091153 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1154 const RenderingStats& stats =
1155 rendering_stats_instrumentation_->GetRenderingStats();
1156 paint_time_counter_->SavePaintTime(stats.total_paint_time);
1157 }
1158
[email protected]37349bc2013-06-04 01:31:521159 bool is_new_trace;
1160 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1161 if (is_new_trace) {
[email protected]ff1211d2013-06-07 01:58:351162 if (pending_tree_)
1163 LayerTreeHostCommon::CallFunctionForSubtree<
1164 DidBeginTracingFunctor, LayerImpl>(
1165 pending_tree_->root_layer());
1166 LayerTreeHostCommon::CallFunctionForSubtree<
1167 DidBeginTracingFunctor, LayerImpl>(
1168 active_tree_->root_layer());
[email protected]37349bc2013-06-04 01:31:521169 }
1170
[email protected]9197dbcb2013-05-15 20:28:511171 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1172 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this,
1173 TracedValue::FromValue(AsValue().release()));
[email protected]131a0c22013-02-12 18:31:081174
[email protected]c1bb5af2013-03-13 19:06:271175 // Because the contents of the HUD depend on everything else in the frame, the
1176 // contents of its texture are updated as the last thing before the frame is
1177 // drawn.
1178 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541179 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141180
[email protected]b09c1942013-05-10 00:06:371181 if (output_surface_->ForcedDrawToSoftwareDevice()) {
1182 scoped_ptr<SoftwareRenderer> temp_software_renderer =
1183 SoftwareRenderer::Create(this, output_surface_.get(), NULL);
1184 temp_software_renderer->DrawFrame(&frame->render_passes);
1185 } else {
1186 renderer_->DrawFrame(&frame->render_passes);
1187 }
[email protected]c1bb5af2013-03-13 19:06:271188 // The render passes should be consumed by the renderer.
1189 DCHECK(frame->render_passes.empty());
1190 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141191
[email protected]c1bb5af2013-03-13 19:06:271192 // The next frame should start by assuming nothing has changed, and changes
1193 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541194 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271195 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1196 DidDrawDamagedArea();
1197 }
1198 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]94f206c12012-08-25 00:09:141199}
1200
[email protected]c1bb5af2013-03-13 19:06:271201void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1202 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1203 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521204
[email protected]c1bb5af2013-03-13 19:06:271205 // Once all layers have been drawn, pending texture uploads should no
1206 // longer block future uploads.
[email protected]fbe89f72013-05-21 07:24:241207 if (resource_provider_)
1208 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141209}
1210
[email protected]c1bb5af2013-03-13 19:06:271211void LayerTreeHostImpl::FinishAllRendering() {
1212 if (renderer_)
1213 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141214}
1215
[email protected]c1bb5af2013-03-13 19:06:271216bool LayerTreeHostImpl::IsContextLost() {
1217 DCHECK(proxy_->IsImplThread());
1218 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141219}
1220
[email protected]c1bb5af2013-03-13 19:06:271221const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1222 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141223}
1224
[email protected]e0341352013-04-06 05:01:201225bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1226 if (frame.has_no_damage)
1227 return false;
[email protected]c5c506382013-04-30 04:42:161228 renderer_->SwapBuffers(frame.latency_info);
1229 active_tree_->ClearLatencyInfo();
1230 return true;
[email protected]94f206c12012-08-25 00:09:141231}
1232
[email protected]c8cbae72013-05-23 10:45:031233void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451234 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031235 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451236}
1237
[email protected]94c40e6292013-05-24 22:01:501238float LayerTreeHostImpl::DeviceScaleFactor() const {
1239 return device_scale_factor_;
1240}
1241
[email protected]ffb2720f2013-03-15 19:18:371242gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
1243 gfx::SizeF dip_size =
[email protected]f224cc92013-06-06 23:23:321244 gfx::ScaleSize(device_viewport_size(), 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371245
1246 // The clip layer should be used if non-overlay scrollbars may exist since
1247 // it adjusts for them.
1248 LayerImpl* clip_layer = active_tree_->RootClipLayer();
[email protected]8e0176d2013-03-21 03:14:521249 if (!Settings().solid_color_scrollbars && clip_layer &&
[email protected]ffb2720f2013-03-15 19:18:371250 clip_layer->masks_to_bounds())
1251 dip_size = clip_layer->bounds();
1252
[email protected]ed511b8d2013-03-25 03:29:291253 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371254 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401255 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291256 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371257}
1258
[email protected]c1bb5af2013-03-13 19:06:271259const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1260 return settings();
[email protected]493067512012-09-19 23:34:101261}
1262
[email protected]c1bb5af2013-03-13 19:06:271263void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011264 // TODO(jamesr): The renderer_ check is needed to make some of the
1265 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1266 // important) in production. We should adjust the test to not need this.
1267 if (renderer_)
1268 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141269}
1270
[email protected]c1bb5af2013-03-13 19:06:271271void LayerTreeHostImpl::OnSwapBuffersComplete() {
1272 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]94f206c12012-08-25 00:09:141273}
1274
[email protected]c1bb5af2013-03-13 19:06:271275void LayerTreeHostImpl::Readback(void* pixels,
1276 gfx::Rect rect_in_device_viewport) {
1277 DCHECK(renderer_);
1278 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141279}
1280
[email protected]59adb112013-04-09 04:48:441281bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341282 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011283}
1284
[email protected]c1bb5af2013-03-13 19:06:271285LayerImpl* LayerTreeHostImpl::RootLayer() const {
1286 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081287}
1288
[email protected]c1bb5af2013-03-13 19:06:271289LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1290 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081291}
1292
[email protected]c1bb5af2013-03-13 19:06:271293LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1294 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081295}
1296
[email protected]94f206c12012-08-25 00:09:141297// Content layers can be either directly scrollable or contained in an outer
1298// scrolling layer which applies the scroll transform. Given a content layer,
1299// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271300static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1301 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141302 return 0;
[email protected]c1bb5af2013-03-13 19:06:271303
1304 if (layer_impl->scrollable())
1305 return layer_impl;
1306
1307 if (layer_impl->DrawsContent() &&
1308 layer_impl->parent() &&
1309 layer_impl->parent()->scrollable())
1310 return layer_impl->parent();
1311
1312 return 0;
[email protected]94f206c12012-08-25 00:09:141313}
1314
[email protected]c1bb5af2013-03-13 19:06:271315void LayerTreeHostImpl::CreatePendingTree() {
1316 CHECK(!pending_tree_);
1317 if (recycle_tree_)
1318 recycle_tree_.swap(pending_tree_);
1319 else
1320 pending_tree_ = LayerTreeImpl::create(this);
1321 client_->OnCanDrawStateChanged(CanDraw());
1322 client_->OnHasPendingTreeStateChanged(pending_tree_);
1323 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1324 TRACE_EVENT_ASYNC_STEP0("cc",
1325 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271326}
1327
[email protected]c1bb5af2013-03-13 19:06:271328void LayerTreeHostImpl::CheckForCompletedTileUploads() {
1329 DCHECK(!client_->IsInsideDraw()) <<
1330 "Checking for completed uploads within a draw may trigger "
1331 "spurious redraws.";
1332 if (tile_manager_)
1333 tile_manager_->CheckForCompletedTileUploads();
[email protected]eabe5002013-01-12 22:07:481334}
1335
[email protected]c1bb5af2013-03-13 19:06:271336bool LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1337 if (!pending_tree_)
1338 return false;
[email protected]2e7ca422012-12-20 02:57:271339
[email protected]fbe89f72013-05-21 07:24:241340 CHECK(settings_.impl_side_painting);
[email protected]2ae038b2013-01-28 12:52:091341
[email protected]fcb846d2013-05-22 01:42:361342 // TODO(enne): This needs to be moved somewhere else (post-animate?)
[email protected]7d19dc342013-05-02 22:02:041343 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:261344 // Note: This will likely cause ManageTiles to be needed. However,
1345 // it is only out of date as far as the last commit or the last draw.
1346 // For performance reasons, don't call ManageTiles again here.
[email protected]615c78a2013-01-24 23:44:161347
[email protected]f6742f52013-05-08 23:52:221348 TRACE_EVENT_ASYNC_STEP1(
[email protected]fcb846d2013-05-22 01:42:361349 "cc",
1350 "PendingTree", pending_tree_.get(), "activate",
1351 "state", TracedValue::FromValue(ActivationStateAsValue().release()));
[email protected]4f0a5002013-01-28 13:02:271352
[email protected]fcb846d2013-05-22 01:42:361353 if (tile_manager_) {
1354 tile_manager_->CheckForCompletedTileUploads();
1355 if (!tile_manager_->AreTilesRequiredForActivationReady())
[email protected]fbe89f72013-05-21 07:24:241356 return false;
1357 }
[email protected]2e7ca422012-12-20 02:57:271358
[email protected]c1bb5af2013-03-13 19:06:271359 ActivatePendingTree();
1360 return true;
[email protected]2e7ca422012-12-20 02:57:271361}
1362
[email protected]c1bb5af2013-03-13 19:06:271363void LayerTreeHostImpl::ActivatePendingTree() {
1364 CHECK(pending_tree_);
1365 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351366
[email protected]1960a712013-04-30 17:06:471367 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271368 active_tree_->PushPersistedState(pending_tree_.get());
1369 if (pending_tree_->needs_full_tree_sync()) {
1370 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331371 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271372 active_tree_->DetachLayerTree(),
1373 active_tree_.get()));
1374 }
[email protected]b5651c22013-03-14 15:06:331375 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271376 active_tree_->root_layer());
1377 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511378
[email protected]c1bb5af2013-03-13 19:06:271379 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511380
[email protected]c1bb5af2013-03-13 19:06:271381 // Now that we've synced everything from the pending tree to the active
1382 // tree, rename the pending tree the recycle tree so we can reuse it on the
1383 // next sync.
1384 pending_tree_.swap(recycle_tree_);
1385 recycle_tree_->ClearRenderSurfaces();
[email protected]48871fc2013-01-23 07:36:511386
[email protected]1960a712013-04-30 17:06:471387 active_tree_->SetRootLayerScrollOffsetDelegate(
1388 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271389 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221390
[email protected]c1bb5af2013-03-13 19:06:271391 // Reduce wasted memory now that unlinked resources are guaranteed not
1392 // to be used.
1393 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411394
[email protected]c1bb5af2013-03-13 19:06:271395 client_->OnCanDrawStateChanged(CanDraw());
1396 client_->OnHasPendingTreeStateChanged(pending_tree_);
1397 client_->SetNeedsRedrawOnImplThread();
1398 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241399
[email protected]a848c102013-03-26 08:59:091400 if (debug_state_.continuous_painting) {
1401 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431402 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091403 paint_time_counter_->SavePaintTime(
[email protected]41abb6d92013-04-26 16:38:501404 stats.total_paint_time + stats.total_record_time +
[email protected]a848c102013-03-26 08:59:091405 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271406 }
[email protected]2a61ad52013-05-13 14:01:291407
1408 client_->DidActivatePendingTree();
[email protected]2e7ca422012-12-20 02:57:271409}
1410
[email protected]c1bb5af2013-03-13 19:06:271411void LayerTreeHostImpl::SetVisible(bool visible) {
1412 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141413
[email protected]c1bb5af2013-03-13 19:06:271414 if (visible_ == visible)
1415 return;
1416 visible_ = visible;
1417 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121418 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141419
[email protected]c1bb5af2013-03-13 19:06:271420 if (!renderer_)
1421 return;
[email protected]94f206c12012-08-25 00:09:141422
[email protected]c1bb5af2013-03-13 19:06:271423 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141424}
1425
[email protected]206a3922013-05-17 06:34:121426ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
1427 if (!debug_state_.rasterize_only_visible_content)
1428 return managed_memory_policy_;
1429
1430 ManagedMemoryPolicy actual = managed_memory_policy_;
1431 actual.priority_cutoff_when_not_visible =
1432 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1433 actual.priority_cutoff_when_visible =
1434 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1435 return actual;
1436}
1437
[email protected]c1bb5af2013-03-13 19:06:271438bool LayerTreeHostImpl::InitializeRenderer(
1439 scoped_ptr<OutputSurface> output_surface) {
1440 // Since we will create a new resource provider, we cannot continue to use
1441 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1442 // before we destroy the old resource provider.
1443 if (active_tree_->root_layer())
1444 ClearRenderSurfaces();
[email protected]ff1211d2013-06-07 01:58:351445 if (active_tree_->root_layer())
1446 SendDidLoseOutputSurfaceRecursive(active_tree_->root_layer());
1447 if (pending_tree_ && pending_tree_->root_layer())
1448 SendDidLoseOutputSurfaceRecursive(pending_tree_->root_layer());
1449 if (recycle_tree_ && recycle_tree_->root_layer())
1450 SendDidLoseOutputSurfaceRecursive(recycle_tree_->root_layer());
[email protected]7ba3ca72013-04-11 06:37:251451 if (resource_provider_)
1452 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401453
[email protected]c1bb5af2013-03-13 19:06:271454 // Note: order is important here.
1455 renderer_.reset();
1456 tile_manager_.reset();
1457 resource_provider_.reset();
1458 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141459
[email protected]c1bb5af2013-03-13 19:06:271460 if (!output_surface->BindToClient(this))
1461 return false;
[email protected]be3181652012-09-25 13:02:131462
[email protected]2b154b22013-06-07 09:03:271463 return DoInitializeRenderer(output_surface.Pass(),
1464 false /* is_deferred_init */);
1465}
1466
1467bool LayerTreeHostImpl::DoInitializeRenderer(
1468 scoped_ptr<OutputSurface> output_surface,
1469 bool is_deferred_init) {
1470 if (output_surface->capabilities().deferred_gl_initialization &&
1471 !is_deferred_init) {
[email protected]fbe89f72013-05-21 07:24:241472 // TODO(joth): Defer creating the Renderer too, until GL is initialized.
1473 // See http://crbug.com/230197
1474 renderer_ = SoftwareRenderer::Create(this, output_surface.get(), NULL);
1475 } else {
1476 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1477 output_surface.get(), settings_.highp_threshold_min);
1478 if (!resource_provider)
1479 return false;
[email protected]be3181652012-09-25 13:02:131480
[email protected]fbe89f72013-05-21 07:24:241481 if (output_surface->capabilities().has_parent_compositor) {
1482 renderer_ = DelegatingRenderer::Create(this, output_surface.get(),
1483 resource_provider.get());
1484 } else if (output_surface->context3d()) {
1485 renderer_ = GLRenderer::Create(this,
1486 output_surface.get(),
1487 resource_provider.get(),
[email protected]bec084292013-05-21 21:31:441488 settings_.highp_threshold_min,
1489 settings_.force_direct_layer_drawing);
[email protected]fbe89f72013-05-21 07:24:241490 } else if (output_surface->software_device()) {
1491 renderer_ = SoftwareRenderer::Create(this,
1492 output_surface.get(),
[email protected]c1bb5af2013-03-13 19:06:271493 resource_provider.get());
[email protected]fbe89f72013-05-21 07:24:241494 }
1495 if (!renderer_)
1496 return false;
[email protected]be3181652012-09-25 13:02:131497
[email protected]6123930c2013-05-23 06:48:371498 if (settings_.impl_side_painting) {
1499 bool using_map_image = GetRendererCapabilities().using_map_image;
1500 tile_manager_ = TileManager::Create(this,
1501 resource_provider.get(),
1502 settings_.num_raster_threads,
1503 settings_.use_color_estimator,
1504 rendering_stats_instrumentation_,
1505 using_map_image);
1506 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
1507 }
1508
[email protected]fbe89f72013-05-21 07:24:241509 resource_provider_ = resource_provider.Pass();
1510 }
1511
[email protected]c1bb5af2013-03-13 19:06:271512 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141513
[email protected]c1bb5af2013-03-13 19:06:271514 if (!visible_)
1515 renderer_->SetVisible(visible_);
[email protected]94f206c12012-08-25 00:09:141516
[email protected]c1bb5af2013-03-13 19:06:271517 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211518
[email protected]c1bb5af2013-03-13 19:06:271519 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1520 // to be initialized to get max texture size.
1521 active_tree_->set_needs_update_draw_properties();
1522 if (pending_tree_)
1523 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161524
[email protected]c1bb5af2013-03-13 19:06:271525 return true;
[email protected]94f206c12012-08-25 00:09:141526}
1527
[email protected]2b154b22013-06-07 09:03:271528bool LayerTreeHostImpl::DeferredInitialize(
1529 scoped_refptr<ContextProvider> offscreen_context_provider) {
1530 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1531 DCHECK(output_surface_->context3d());
1532
1533 // TODO(boliu): This is temporary until proper resource clean up is possible
1534 // without resetting |tile_manager_| or |resource_provider_|.
1535 DCHECK(!resource_provider_);
1536
1537 bool success =
1538 DoInitializeRenderer(output_surface_.Pass(), true /* is_deferred_init */);
1539 client_->DidTryInitializeRendererOnImplThread(success,
1540 offscreen_context_provider);
1541 return success;
1542}
1543
[email protected]18ce59702013-04-09 04:58:401544void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1545 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271546 return;
[email protected]94f206c12012-08-25 00:09:141547
[email protected]c1bb5af2013-03-13 19:06:271548 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1549 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271550
[email protected]c1bb5af2013-03-13 19:06:271551 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141552
[email protected]c1bb5af2013-03-13 19:06:271553 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141554
[email protected]c1bb5af2013-03-13 19:06:271555 if (renderer_)
1556 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211557
[email protected]c1bb5af2013-03-13 19:06:271558 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441559 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141560}
1561
[email protected]d9083762013-03-24 01:36:401562void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1563 if (overdraw_bottom_height == overdraw_bottom_height_)
1564 return;
1565 overdraw_bottom_height_ = overdraw_bottom_height;
1566
1567 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441568 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401569}
1570
[email protected]c1bb5af2013-03-13 19:06:271571void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1572 if (device_scale_factor == device_scale_factor_)
1573 return;
1574 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271575
[email protected]94c40e6292013-05-24 22:01:501576 if (renderer_)
1577 renderer_->ViewportChanged();
1578
[email protected]c1bb5af2013-03-13 19:06:271579 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441580 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141581}
1582
[email protected]f224cc92013-06-06 23:23:321583gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1584 if (external_viewport_.IsEmpty())
1585 return gfx::Rect(device_viewport_size_);
1586
1587 return external_viewport_;
1588}
1589
1590const gfx::Transform& LayerTreeHostImpl::DeviceTransform() const {
1591 return external_transform_;
1592}
1593
[email protected]c1bb5af2013-03-13 19:06:271594void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1595 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141596}
1597
[email protected]59adb112013-04-09 04:48:441598void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271599 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441600 active_tree_->set_needs_update_draw_properties();
1601 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141602}
1603
[email protected]c1bb5af2013-03-13 19:06:271604bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041605 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341606 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141607}
1608
[email protected]200a9c062013-05-20 04:34:371609void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1610 DCHECK(input_handler_client_ == NULL);
1611 input_handler_client_ = client;
1612}
1613
[email protected]5ff3c9782013-04-29 17:35:121614InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1615 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291616 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141617
[email protected]c1bb5af2013-03-13 19:06:271618 if (top_controls_manager_)
1619 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381620
[email protected]c1bb5af2013-03-13 19:06:271621 DCHECK(!CurrentlyScrollingLayer());
1622 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141623
[email protected]c1bb5af2013-03-13 19:06:271624 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141625 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141626
[email protected]c1bb5af2013-03-13 19:06:271627 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1628 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141629
[email protected]c1bb5af2013-03-13 19:06:271630 // First find out which layer was hit from the saved list of visible layers
1631 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391632 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271633 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521634
[email protected]c1bb5af2013-03-13 19:06:271635 // Walk up the hierarchy and look for a scrollable layer.
1636 LayerImpl* potentially_scrolling_layer_impl = 0;
1637 for (; layer_impl; layer_impl = layer_impl->parent()) {
1638 // The content layer can also block attempts to scroll outside the main
1639 // thread.
1640 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1641 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431642 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271643 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1644 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141645 }
1646
[email protected]c1bb5af2013-03-13 19:06:271647 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1648 if (!scroll_layer_impl)
1649 continue;
[email protected]94f206c12012-08-25 00:09:141650
[email protected]c1bb5af2013-03-13 19:06:271651 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141652
[email protected]c1bb5af2013-03-13 19:06:271653 // If any layer wants to divert the scroll event to the main thread, abort.
1654 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431655 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271656 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1657 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141658 }
1659
[email protected]c1bb5af2013-03-13 19:06:271660 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1661 potentially_scrolling_layer_impl = scroll_layer_impl;
1662 }
1663
1664 // When hiding top controls is enabled and the controls are hidden or
1665 // overlaying the content, force scrolls to be enabled on the root layer to
1666 // allow bringing the top controls back into view.
1667 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1668 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521669 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271670 potentially_scrolling_layer_impl = RootScrollLayer();
1671 }
1672
1673 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201674 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271675 potentially_scrolling_layer_impl);
1676 should_bubble_scrolls_ = (type != NonBubblingGesture);
1677 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431678 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271679 client_->RenewTreePriority();
1680 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
1681 return ScrollStarted;
1682 }
1683 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141684}
1685
[email protected]c1bb5af2013-03-13 19:06:271686gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1687 LayerImpl* layer_impl,
1688 float scale_from_viewport_to_screen_space,
1689 gfx::PointF viewport_point,
1690 gfx::Vector2dF viewport_delta) {
1691 // Layers with non-invertible screen space transforms should not have passed
1692 // the scroll hit test in the first place.
1693 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1694 gfx::Transform inverse_screen_space_transform(
1695 gfx::Transform::kSkipInitialization);
1696 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1697 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351698 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1699 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271700 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141701
[email protected]c1bb5af2013-03-13 19:06:271702 gfx::PointF screen_space_point =
1703 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141704
[email protected]c1bb5af2013-03-13 19:06:271705 gfx::Vector2dF screen_space_delta = viewport_delta;
1706 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1707
1708 // First project the scroll start and end points to local layer space to find
1709 // the scroll delta in layer coordinates.
1710 bool start_clipped, end_clipped;
1711 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1712 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211713 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271714 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211715 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271716 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211717 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271718 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211719 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271720
1721 // In general scroll point coordinates should not get clipped.
1722 DCHECK(!start_clipped);
1723 DCHECK(!end_clipped);
1724 if (start_clipped || end_clipped)
1725 return gfx::Vector2dF();
1726
1727 // local_start_point and local_end_point are in content space but we want to
1728 // move them to layer space for scrolling.
1729 float width_scale = 1.f / layer_impl->contents_scale_x();
1730 float height_scale = 1.f / layer_impl->contents_scale_y();
1731 local_start_point.Scale(width_scale, height_scale);
1732 local_end_point.Scale(width_scale, height_scale);
1733
1734 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:471735 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:271736 layer_impl->ScrollBy(local_end_point - local_start_point);
1737
1738 // Get the end point in the layer's content space so we can apply its
1739 // ScreenSpaceTransform.
1740 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:471741 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:271742 previous_delta;
1743 gfx::PointF actual_local_content_end_point =
1744 gfx::ScalePoint(actual_local_end_point,
1745 1.f / width_scale,
1746 1.f / height_scale);
1747
1748 // Calculate the applied scroll delta in viewport space coordinates.
1749 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211750 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271751 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211752 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271753 DCHECK(!end_clipped);
1754 if (end_clipped)
1755 return gfx::Vector2dF();
1756 gfx::PointF actual_viewport_end_point =
1757 gfx::ScalePoint(actual_screen_space_end_point,
1758 1.f / scale_from_viewport_to_screen_space);
1759 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141760}
1761
[email protected]c1bb5af2013-03-13 19:06:271762static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1763 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:471764 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271765 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:471766 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:271767}
1768
1769bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1770 gfx::Vector2dF scroll_delta) {
1771 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1772 if (!CurrentlyScrollingLayer())
1773 return false;
1774
1775 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:531776 gfx::Vector2dF unused_root_delta;
[email protected]c1bb5af2013-03-13 19:06:271777 bool did_scroll = false;
[email protected]60b4d252013-03-23 18:49:421778 bool consume_by_top_controls = top_controls_manager_ &&
1779 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061780
[email protected]c1bb5af2013-03-13 19:06:271781 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1782 layer_impl;
1783 layer_impl = layer_impl->parent()) {
1784 if (!layer_impl->scrollable())
1785 continue;
1786
[email protected]a2b5ded2013-05-20 21:32:531787 if (layer_impl == RootScrollLayer()) {
1788 // Only allow bubble scrolling when the scroll is in the direction to make
1789 // the top controls visible.
1790 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1791 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1792 UpdateMaxScrollOffset();
1793 }
1794 // Track root layer deltas for reporting overscroll.
1795 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:421796 }
1797
[email protected]c1bb5af2013-03-13 19:06:271798 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271799 // Gesture events need to be transformed from viewport coordinates to local
1800 // layer coordinates so that the scrolling contents exactly follow the
1801 // user's finger. In contrast, wheel events represent a fixed amount of
1802 // scrolling so we can just apply them directly.
1803 if (!wheel_scrolling_) {
1804 float scale_from_viewport_to_screen_space = device_scale_factor_;
1805 applied_delta =
1806 ScrollLayerWithViewportSpaceDelta(layer_impl,
1807 scale_from_viewport_to_screen_space,
1808 viewport_point, pending_delta);
1809 } else {
1810 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141811 }
[email protected]94f206c12012-08-25 00:09:141812
[email protected]c1bb5af2013-03-13 19:06:271813 // If the layer wasn't able to move, try the next one in the hierarchy.
1814 float move_threshold_squared = 0.1f * 0.1f;
1815 if (applied_delta.LengthSquared() < move_threshold_squared) {
1816 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1817 continue;
1818 else
1819 break;
[email protected]94f206c12012-08-25 00:09:141820 }
[email protected]a2b5ded2013-05-20 21:32:531821
1822 if (layer_impl == RootScrollLayer())
1823 unused_root_delta.Subtract(applied_delta);
1824
[email protected]c1bb5af2013-03-13 19:06:271825 did_scroll = true;
1826 did_lock_scrolling_layer_ = true;
1827 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:201828 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:271829 break;
[email protected]94f206c12012-08-25 00:09:141830 }
[email protected]94f206c12012-08-25 00:09:141831
[email protected]c1bb5af2013-03-13 19:06:271832 // If the applied delta is within 45 degrees of the input delta, bail out to
1833 // make it easier to scroll just one layer in one direction without
1834 // affecting any of its parents.
1835 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:211836 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:271837 applied_delta, pending_delta) < angle_threshold) {
1838 pending_delta = gfx::Vector2d();
1839 break;
[email protected]4a23c374c2012-12-08 08:38:551840 }
[email protected]c1bb5af2013-03-13 19:06:271841
1842 // Allow further movement only on an axis perpendicular to the direction in
1843 // which the layer moved.
1844 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:211845 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:271846
[email protected]a2b5ded2013-05-20 21:32:531847 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:271848 break;
1849 }
1850
1851 if (did_scroll) {
1852 client_->SetNeedsCommitOnImplThread();
1853 client_->SetNeedsRedrawOnImplThread();
1854 client_->RenewTreePriority();
[email protected]a2b5ded2013-05-20 21:32:531855
1856 // Scrolling of any layer will reset root overscroll accumulation.
1857 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]c1bb5af2013-03-13 19:06:271858 }
[email protected]a2b5ded2013-05-20 21:32:531859
1860 accumulated_root_overscroll_ += unused_root_delta;
1861 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
1862 if (did_overscroll && input_handler_client_) {
1863 input_handler_client_->DidOverscroll(accumulated_root_overscroll_,
1864 current_fling_velocity_);
1865 }
1866
[email protected]c1bb5af2013-03-13 19:06:271867 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:551868}
1869
[email protected]be782f52013-03-23 21:36:141870// This implements scrolling by page as described here:
1871// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
1872// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:491873bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
1874 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:141875 DCHECK(wheel_scrolling_);
1876
1877 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1878 layer_impl;
1879 layer_impl = layer_impl->parent()) {
1880 if (!layer_impl->scrollable())
1881 continue;
1882
1883 if (!layer_impl->vertical_scrollbar_layer())
1884 continue;
1885
1886 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
1887
1888 // These magical values match WebKit and are designed to scroll nearly the
1889 // entire visible content height but leave a bit of overlap.
1890 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:491891 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:141892 page = -page;
1893
1894 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
1895
1896 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
1897
1898 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:141899 client_->SetNeedsCommitOnImplThread();
1900 client_->SetNeedsRedrawOnImplThread();
1901 client_->RenewTreePriority();
1902 return true;
1903 }
1904
1905 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
1906 }
1907
1908 return false;
1909}
1910
[email protected]1960a712013-04-30 17:06:471911void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
1912 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
1913 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
1914 active_tree_->SetRootLayerScrollOffsetDelegate(
1915 root_layer_scroll_offset_delegate_);
1916}
1917
1918void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
1919 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
1920 client_->SetNeedsCommitOnImplThread();
1921}
1922
[email protected]c1bb5af2013-03-13 19:06:271923void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
1924 active_tree_->ClearCurrentlyScrollingLayer();
1925 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:531926 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:201927 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:141928}
1929
[email protected]c1bb5af2013-03-13 19:06:271930void LayerTreeHostImpl::ScrollEnd() {
1931 if (top_controls_manager_)
1932 top_controls_manager_->ScrollEnd();
1933 ClearCurrentlyScrollingLayer();
[email protected]fb7425a2013-04-22 16:28:551934 StartScrollbarAnimation(CurrentFrameTimeTicks());
[email protected]94f206c12012-08-25 00:09:141935}
1936
[email protected]5ff3c9782013-04-29 17:35:121937InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]7c45d8152013-04-23 18:27:211938 if (active_tree_->CurrentlyScrollingLayer())
1939 return ScrollStarted;
1940
1941 return ScrollIgnored;
1942}
1943
[email protected]6e921bd2013-04-29 21:10:201944void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
1945 current_fling_velocity_ = velocity;
1946}
1947
[email protected]c1bb5af2013-03-13 19:06:271948void LayerTreeHostImpl::PinchGestureBegin() {
1949 pinch_gesture_active_ = true;
1950 previous_pinch_anchor_ = gfx::Point();
1951 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:141952}
1953
[email protected]c1bb5af2013-03-13 19:06:271954void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
1955 gfx::Point anchor) {
1956 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:281957
[email protected]c1bb5af2013-03-13 19:06:271958 if (!RootScrollLayer())
1959 return;
[email protected]d3afa112012-12-08 06:24:281960
[email protected]c1bb5af2013-03-13 19:06:271961 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1962 // position over the course of the magnify.
1963 float page_scale_delta = active_tree_->page_scale_delta();
1964 gfx::PointF previous_scale_anchor =
1965 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1966 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
1967 page_scale_delta = active_tree_->page_scale_delta();
1968 gfx::PointF new_scale_anchor =
1969 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1970 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
1971
1972 previous_pinch_anchor_ = anchor;
1973
1974 move.Scale(1 / active_tree_->page_scale_factor());
1975
1976 RootScrollLayer()->ScrollBy(move);
1977
[email protected]c1bb5af2013-03-13 19:06:271978 client_->SetNeedsCommitOnImplThread();
1979 client_->SetNeedsRedrawOnImplThread();
1980 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:281981}
1982
[email protected]c1bb5af2013-03-13 19:06:271983void LayerTreeHostImpl::PinchGestureEnd() {
1984 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:271985 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141986}
1987
[email protected]c1bb5af2013-03-13 19:06:271988static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
1989 LayerImpl* layer_impl) {
1990 if (!layer_impl)
1991 return;
[email protected]94f206c12012-08-25 00:09:141992
[email protected]c1bb5af2013-03-13 19:06:271993 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:471994 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271995 if (!scroll_delta.IsZero()) {
1996 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:391997 scroll.layer_id = layer_impl->id();
1998 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:271999 scroll_info->scrolls.push_back(scroll);
2000 layer_impl->SetSentScrollDelta(scroll_delta);
2001 }
[email protected]94f206c12012-08-25 00:09:142002
[email protected]c1bb5af2013-03-13 19:06:272003 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2004 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142005}
2006
[email protected]c1bb5af2013-03-13 19:06:272007scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2008 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302009
[email protected]c1bb5af2013-03-13 19:06:272010 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392011 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2012 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302013
[email protected]c1bb5af2013-03-13 19:06:272014 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302015}
2016
[email protected]c1bb5af2013-03-13 19:06:272017void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]878705be2013-04-15 22:44:022018 SetViewportDamage(gfx::Rect(device_viewport_size_));
[email protected]829ad972013-01-28 23:36:102019}
2020
[email protected]c1bb5af2013-03-13 19:06:272021void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2022 if (!page_scale_animation_ || !RootScrollLayer())
2023 return;
2024
2025 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2026 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472027 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272028
2029 active_tree_->SetPageScaleDelta(
2030 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2031 active_tree_->page_scale_factor());
2032 gfx::Vector2dF next_scroll =
2033 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2034
2035 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2036 client_->SetNeedsRedrawOnImplThread();
2037
2038 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2039 page_scale_animation_.reset();
2040 client_->SetNeedsCommitOnImplThread();
2041 client_->RenewTreePriority();
2042 }
[email protected]829ad972013-01-28 23:36:102043}
2044
[email protected]ffb2720f2013-03-15 19:18:372045void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2046 if (!top_controls_manager_ || !RootScrollLayer())
2047 return;
2048 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2049 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172050 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2051 return;
[email protected]ffb2720f2013-03-15 19:18:372052 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2053 scroll, 1.f / active_tree_->total_page_scale_factor()));
2054}
2055
[email protected]c1bb5af2013-03-13 19:06:272056void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2057 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522058 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272059 animation_registrar_->active_animation_controllers().empty() ||
2060 !active_tree_->root_layer())
2061 return;
2062
2063 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2064
2065 last_animation_time_ = wall_clock_time;
2066 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2067
2068 AnimationRegistrar::AnimationControllerMap copy =
2069 animation_registrar_->active_animation_controllers();
2070 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2071 iter != copy.end();
2072 ++iter)
2073 (*iter).second->Animate(monotonic_seconds);
2074
2075 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082076}
2077
[email protected]3d9f7432013-04-06 00:35:182078void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522079 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272080 animation_registrar_->active_animation_controllers().empty() ||
2081 !active_tree_->root_layer())
2082 return;
2083
2084 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2085 scoped_ptr<AnimationEventsVector> events =
2086 make_scoped_ptr(new AnimationEventsVector);
2087 AnimationRegistrar::AnimationControllerMap copy =
2088 animation_registrar_->active_animation_controllers();
2089 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2090 iter != copy.end();
2091 ++iter)
[email protected]3d9f7432013-04-06 00:35:182092 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272093
2094 if (!events->empty()) {
2095 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2096 last_animation_time_);
2097 }
[email protected]131a0c22013-02-12 18:31:082098}
2099
[email protected]c1bb5af2013-03-13 19:06:272100base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2101 return base::TimeDelta::FromSeconds(1);
2102}
2103
[email protected]ff1211d2013-06-07 01:58:352104void LayerTreeHostImpl::SendDidLoseOutputSurfaceRecursive(LayerImpl* current) {
2105 DCHECK(current);
2106 current->DidLoseOutputSurface();
2107 if (current->mask_layer())
2108 SendDidLoseOutputSurfaceRecursive(current->mask_layer());
2109 if (current->replica_layer())
2110 SendDidLoseOutputSurfaceRecursive(current->replica_layer());
2111 for (size_t i = 0; i < current->children().size(); ++i)
2112 SendDidLoseOutputSurfaceRecursive(current->children()[i]);
2113}
2114
[email protected]c1bb5af2013-03-13 19:06:272115void LayerTreeHostImpl::ClearRenderSurfaces() {
2116 active_tree_->ClearRenderSurfaces();
2117 if (pending_tree_)
2118 pending_tree_->ClearRenderSurfaces();
2119}
2120
2121std::string LayerTreeHostImpl::LayerTreeAsText() const {
2122 std::string str;
2123 if (active_tree_->root_layer()) {
2124 str = active_tree_->root_layer()->LayerTreeAsText();
2125 str += "RenderSurfaces:\n";
2126 DumpRenderSurfaces(&str, 1, active_tree_->root_layer());
2127 }
2128 return str;
2129}
2130
2131std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2132 std::string str;
2133 if (active_tree_->root_layer()) {
2134 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2135 base::JSONWriter::WriteWithOptions(
2136 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2137 }
2138 return str;
2139}
2140
2141void LayerTreeHostImpl::DumpRenderSurfaces(std::string* str,
2142 int indent,
2143 const LayerImpl* layer) const {
2144 if (layer->render_surface())
2145 layer->render_surface()->DumpSurface(str, indent);
2146
2147 for (size_t i = 0; i < layer->children().size(); ++i)
2148 DumpRenderSurfaces(str, indent, layer->children()[i]);
2149}
2150
2151int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362152 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272153}
2154
[email protected]c1bb5af2013-03-13 19:06:272155void LayerTreeHostImpl::SendManagedMemoryStats(
2156 size_t memory_visible_bytes,
2157 size_t memory_visible_and_nearby_bytes,
2158 size_t memory_use_bytes) {
2159 if (!renderer_)
2160 return;
2161
2162 // Round the numbers being sent up to the next 8MB, to throttle the rate
2163 // at which we spam the GPU process.
2164 static const size_t rounding_step = 8 * 1024 * 1024;
2165 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2166 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2167 rounding_step);
2168 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2169 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2170 last_sent_memory_visible_and_nearby_bytes_ ==
2171 memory_visible_and_nearby_bytes &&
2172 last_sent_memory_use_bytes_ == memory_use_bytes) {
2173 return;
2174 }
2175 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2176 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2177 last_sent_memory_use_bytes_ = memory_use_bytes;
2178
2179 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2180 last_sent_memory_visible_and_nearby_bytes_,
2181 last_sent_memory_use_bytes_);
2182}
2183
2184void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2185 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2186}
2187
2188void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2189 base::TimeTicks time) {
2190 if (!layer)
2191 return;
2192
2193 ScrollbarAnimationController* scrollbar_controller =
2194 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352195 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202196 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422197 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2198 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272199 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202200 }
[email protected]c1bb5af2013-03-13 19:06:272201
2202 for (size_t i = 0; i < layer->children().size(); ++i)
2203 AnimateScrollbarsRecursive(layer->children()[i], time);
2204}
2205
[email protected]0fc818e2013-03-18 06:45:202206void LayerTreeHostImpl::StartScrollbarAnimation(base::TimeTicks time) {
2207 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
2208 StartScrollbarAnimationRecursive(RootLayer(), time);
2209}
2210
2211void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2212 base::TimeTicks time) {
2213 if (!layer)
2214 return;
2215
2216 ScrollbarAnimationController* scrollbar_controller =
2217 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352218 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2219 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202220 if (delay > base::TimeDelta())
2221 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352222 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202223 client_->SetNeedsRedrawOnImplThread();
2224 }
2225
2226 for (size_t i = 0; i < layer->children().size(); ++i)
2227 StartScrollbarAnimationRecursive(layer->children()[i], time);
2228}
2229
[email protected]c1bb5af2013-03-13 19:06:272230void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2231 if (!tile_manager_)
2232 return;
2233
2234 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2235 if (new_state.tree_priority == priority)
2236 return;
2237
2238 new_state.tree_priority = priority;
2239 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:262240 manage_tiles_needed_ = true;
[email protected]c1bb5af2013-03-13 19:06:272241}
2242
[email protected]8347d692013-05-17 23:22:382243void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552244 current_frame_timeticks_ = base::TimeTicks();
2245 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272246}
2247
[email protected]fb7425a2013-04-22 16:28:552248static void UpdateCurrentFrameTime(base::TimeTicks* ticks, base::Time* now) {
2249 if (ticks->is_null()) {
2250 DCHECK(now->is_null());
2251 *ticks = base::TimeTicks::Now();
2252 *now = base::Time::Now();
2253 }
2254}
2255
2256base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2257 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2258 return current_frame_timeticks_;
2259}
2260
2261base::Time LayerTreeHostImpl::CurrentFrameTime() {
2262 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272263 return current_frame_time_;
2264}
2265
2266scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
2267 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222268 if (this->pending_tree_)
2269 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272270 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212271 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272272 if (tile_manager_)
2273 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2274 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222275 if (pending_tree_)
2276 state->Set("pending_tree", pending_tree_->AsValue().release());
2277 return state.PassAs<base::Value>();
2278}
2279
2280scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222281 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2282 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472283 if (tile_manager_)
2284 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272285 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082286}
2287
[email protected]b9dcf43a2013-01-09 00:15:292288// static
[email protected]c1bb5af2013-03-13 19:06:272289LayerImpl* LayerTreeHostImpl::GetNonCompositedContentLayerRecursive(
2290 LayerImpl* layer) {
2291 if (!layer)
[email protected]b9dcf43a2013-01-09 00:15:292292 return NULL;
[email protected]c1bb5af2013-03-13 19:06:272293
2294 if (layer->DrawsContent())
2295 return layer;
2296
[email protected]50761e92013-03-29 20:51:282297 for (LayerImplList::const_iterator it = layer->children().begin();
[email protected]c1bb5af2013-03-13 19:06:272298 it != layer->children().end(); ++it) {
2299 LayerImpl* nccr = GetNonCompositedContentLayerRecursive(*it);
2300 if (nccr)
2301 return nccr;
2302 }
2303
2304 return NULL;
[email protected]b9dcf43a2013-01-09 00:15:292305}
2306
[email protected]c1bb5af2013-03-13 19:06:272307skia::RefPtr<SkPicture> LayerTreeHostImpl::CapturePicture() {
2308 LayerTreeImpl* tree =
2309 pending_tree_ ? pending_tree_.get() : active_tree_.get();
2310 LayerImpl* layer = GetNonCompositedContentLayerRecursive(tree->root_layer());
2311 return layer ? layer->GetPicture() : skia::RefPtr<SkPicture>();
[email protected]b9dcf43a2013-01-09 00:15:292312}
2313
[email protected]c1bb5af2013-03-13 19:06:272314void LayerTreeHostImpl::SetDebugState(const LayerTreeDebugState& debug_state) {
[email protected]846f455b2013-03-18 19:07:412315 if (debug_state_.continuous_painting != debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272316 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242317
[email protected]c1bb5af2013-03-13 19:06:272318 debug_state_ = debug_state;
[email protected]d0d12192013-02-08 19:02:022319}
2320
[email protected]d3143c732012-10-05 19:17:592321} // namespace cc