blob: d384ea716996f05d7171f3ddb04a0a1f284d1c6a [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]131a0c22013-02-12 18:31:0813#include "base/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]1960a712013-04-30 17:06:47166 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27167 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27168 visible_(true),
169 managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34170 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27171 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
172 0,
173 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING),
174 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36175 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29176 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27177 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02178 debug_rect_history_(DebugRectHistory::Create()),
[email protected]d7626ffd2013-03-29 00:17:42179 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27180 last_sent_memory_visible_bytes_(0),
181 last_sent_memory_visible_and_nearby_bytes_(0),
182 last_sent_memory_use_bytes_(0),
[email protected]f224cc92013-06-06 23:23:32183 device_scale_factor_(1.f),
184 overdraw_bottom_height_(0.f),
[email protected]372bad5f2013-03-21 16:38:43185 animation_registrar_(AnimationRegistrar::Create()),
186 rendering_stats_instrumentation_(rendering_stats_instrumentation) {
[email protected]c1bb5af2013-03-13 19:06:27187 DCHECK(proxy_->IsImplThread());
188 DidVisibilityChange(this, visible_);
189
[email protected]8e0176d2013-03-21 03:14:52190 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27191
[email protected]8e0176d2013-03-21 03:14:52192 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27193 top_controls_manager_ =
194 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52195 settings.top_controls_height,
196 settings.top_controls_show_threshold,
197 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27198 }
199
[email protected]8e0176d2013-03-21 03:14:52200 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27201
202 // LTHI always has an active tree.
203 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51204 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
205 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10206}
207
[email protected]c1bb5af2013-03-13 19:06:27208LayerTreeHostImpl::~LayerTreeHostImpl() {
209 DCHECK(proxy_->IsImplThread());
210 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51211 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
212 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27213
[email protected]200a9c062013-05-20 04:34:37214 if (input_handler_client_) {
215 input_handler_client_->WillShutdown();
216 input_handler_client_ = NULL;
217 }
218
[email protected]c1bb5af2013-03-13 19:06:27219 if (active_tree_->root_layer()) {
220 ClearRenderSurfaces();
221 // The layer trees must be destroyed before the layer tree host. We've
222 // made a contract with our animation controllers that the registrar
223 // will outlive them, and we must make good.
224 recycle_tree_.reset();
225 pending_tree_.reset();
226 active_tree_.reset();
227 }
[email protected]94f206c12012-08-25 00:09:14228}
229
[email protected]c1bb5af2013-03-13 19:06:27230void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29231
[email protected]c1bb5af2013-03-13 19:06:27232void LayerTreeHostImpl::CommitComplete() {
233 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08234
[email protected]c1bb5af2013-03-13 19:06:27235 // Impl-side painting needs an update immediately post-commit to have the
236 // opportunity to create tilings. Other paths can call UpdateDrawProperties
237 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52238 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27239 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04240 pending_tree_->UpdateDrawProperties();
[email protected]c1bb5af2013-03-13 19:06:27241 } else {
242 active_tree_->set_needs_update_draw_properties();
243 }
[email protected]3ba4cae2013-01-16 03:58:38244
[email protected]c1bb5af2013-03-13 19:06:27245 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14246}
247
[email protected]c1bb5af2013-03-13 19:06:27248bool LayerTreeHostImpl::CanDraw() {
249 // Note: If you are changing this function or any other function that might
250 // affect the result of CanDraw, make sure to call
251 // client_->OnCanDrawStateChanged in the proper places and update the
252 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14253
[email protected]c1bb5af2013-03-13 19:06:27254 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42255 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
256 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22257 return false;
[email protected]c1bb5af2013-03-13 19:06:27258 }
259 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42260 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
261 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27262 return false;
263 }
264 if (active_tree_->ViewportSizeInvalid()) {
265 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42266 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
267 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27268 return false;
269 }
270 if (!renderer_) {
[email protected]c76faea2013-03-26 07:42:42271 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
272 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27273 return false;
274 }
275 if (active_tree_->ContentsTexturesPurged()) {
276 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42277 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
278 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27279 return false;
280 }
281 return true;
[email protected]2f1acc262012-11-16 21:42:22282}
283
[email protected]c1bb5af2013-03-13 19:06:27284void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
285 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37286 if (input_handler_client_)
287 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27288 AnimatePageScale(monotonic_time);
289 AnimateLayers(monotonic_time, wall_clock_time);
290 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37291 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14292}
293
[email protected]c1bb5af2013-03-13 19:06:27294void LayerTreeHostImpl::ManageTiles() {
295 DCHECK(tile_manager_);
296 tile_manager_->ManageTiles();
297
298 size_t memory_required_bytes;
299 size_t memory_nice_to_have_bytes;
300 size_t memory_used_bytes;
301 tile_manager_->GetMemoryStats(&memory_required_bytes,
302 &memory_nice_to_have_bytes,
303 &memory_used_bytes);
304 SendManagedMemoryStats(memory_required_bytes,
305 memory_nice_to_have_bytes,
306 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15307}
308
[email protected]c1bb5af2013-03-13 19:06:27309void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
310 bool anchor_point,
311 float page_scale,
312 base::TimeTicks start_time,
313 base::TimeDelta duration) {
314 if (!RootScrollLayer())
315 return;
316
317 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47318 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27319 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51320 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27321
322 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40323
324 // Easing constants experimentally determined.
325 scoped_ptr<TimingFunction> timing_function =
326 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
327
[email protected]c1bb5af2013-03-13 19:06:27328 page_scale_animation_ =
329 PageScaleAnimation::Create(scroll_total,
330 active_tree_->total_page_scale_factor(),
331 viewport_size,
332 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40333 start_time_seconds,
334 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27335
336 if (anchor_point) {
337 gfx::Vector2dF anchor(target_offset);
338 page_scale_animation_->ZoomWithAnchor(anchor,
339 page_scale,
340 duration.InSecondsF());
341 } else {
342 gfx::Vector2dF scaled_target_offset = target_offset;
343 page_scale_animation_->ZoomTo(scaled_target_offset,
344 page_scale,
345 duration.InSecondsF());
346 }
347
348 client_->SetNeedsRedrawOnImplThread();
349 client_->SetNeedsCommitOnImplThread();
350 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15351}
352
[email protected]c1bb5af2013-03-13 19:06:27353void LayerTreeHostImpl::ScheduleAnimation() {
354 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15355}
356
[email protected]c1bb5af2013-03-13 19:06:27357bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
358 if (!EnsureRenderSurfaceLayerList())
359 return false;
[email protected]f57bbc02012-11-21 07:02:15360
[email protected]c1bb5af2013-03-13 19:06:27361 gfx::PointF device_viewport_point =
362 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15363
[email protected]c1bb5af2013-03-13 19:06:27364 // First find out which layer was hit from the saved list of visible layers
365 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39366 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27367 device_viewport_point,
368 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15369
[email protected]c1bb5af2013-03-13 19:06:27370 // Walk up the hierarchy and look for a layer with a touch event handler
371 // region that the given point hits.
372 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39373 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27374 layer_impl))
375 return true;
376 }
[email protected]f57bbc02012-11-21 07:02:15377
[email protected]c1bb5af2013-03-13 19:06:27378 return false;
379}
380
381void LayerTreeHostImpl::TrackDamageForAllSurfaces(
382 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28383 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27384 // For now, we use damage tracking to compute a global scissor. To do this, we
385 // must compute all damage tracking before drawing anything, so that we know
386 // the root damage rect. The root damage rect is then used to scissor each
387 // surface.
388
389 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06390 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27391 --surface_index) {
392 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
393 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
394 DCHECK(render_surface);
395 render_surface->damage_tracker()->UpdateDamageTrackingState(
396 render_surface->layer_list(),
397 render_surface_layer->id(),
398 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
399 render_surface->content_rect(),
400 render_surface_layer->mask_layer(),
401 render_surface_layer->filters(),
402 render_surface_layer->filter().get());
403 }
404}
405
406void LayerTreeHostImpl::FrameData::AppendRenderPass(
407 scoped_ptr<RenderPass> render_pass) {
408 render_passes_by_id[render_pass->id] = render_pass.get();
409 render_passes.push_back(render_pass.Pass());
410}
411
[email protected]ffbb2212013-06-02 23:47:59412static DrawMode GetDrawMode(OutputSurface* output_surface) {
413 if (output_surface->ForcedDrawToSoftwareDevice()) {
414 return DRAW_MODE_RESOURCELESS_SOFTWARE;
415 } else if (output_surface->context3d()) {
416 return DRAW_MODE_HARDWARE;
417 } else {
418 DCHECK(output_surface->software_device());
419 return DRAW_MODE_SOFTWARE;
420 }
421}
422
[email protected]c1bb5af2013-03-13 19:06:27423static void AppendQuadsForLayer(RenderPass* target_render_pass,
424 LayerImpl* layer,
425 const OcclusionTrackerImpl& occlusion_tracker,
426 AppendQuadsData* append_quads_data) {
427 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49428 QuadCuller quad_culler(&target_render_pass->quad_list,
429 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27430 layer,
431 occlusion_tracker,
432 layer->ShowDebugBorders(),
433 for_surface);
434 layer->AppendQuads(&quad_culler, append_quads_data);
435}
436
437static void AppendQuadsForRenderSurfaceLayer(
438 RenderPass* target_render_pass,
439 LayerImpl* layer,
440 const RenderPass* contributing_render_pass,
441 const OcclusionTrackerImpl& occlusion_tracker,
442 AppendQuadsData* append_quads_data) {
443 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49444 QuadCuller quad_culler(&target_render_pass->quad_list,
445 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27446 layer,
447 occlusion_tracker,
448 layer->ShowDebugBorders(),
449 for_surface);
450
451 bool is_replica = false;
452 layer->render_surface()->AppendQuads(&quad_culler,
453 append_quads_data,
454 is_replica,
455 contributing_render_pass->id);
456
457 // Add replica after the surface so that it appears below the surface.
458 if (layer->has_replica()) {
459 is_replica = true;
460 layer->render_surface()->AppendQuads(&quad_culler,
461 append_quads_data,
462 is_replica,
463 contributing_render_pass->id);
464 }
465}
466
467static void AppendQuadsToFillScreen(
468 RenderPass* target_render_pass,
469 LayerImpl* root_layer,
470 SkColor screen_background_color,
471 const OcclusionTrackerImpl& occlusion_tracker) {
472 if (!root_layer || !SkColorGetA(screen_background_color))
473 return;
474
475 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
476 if (fill_region.IsEmpty())
477 return;
478
479 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49480 QuadCuller quad_culler(&target_render_pass->quad_list,
481 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27482 root_layer,
483 occlusion_tracker,
484 root_layer->ShowDebugBorders(),
485 for_surface);
486
487 // Manually create the quad state for the gutter quads, as the root layer
488 // doesn't have any bounds and so can't generate this itself.
489 // TODO(danakj): Make the gutter quads generated by the solid color layer
490 // (make it smarter about generating quads to fill unoccluded areas).
491
492 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
493 float opacity = 1.f;
494 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49495 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27496 shared_quad_state->SetAll(root_layer->draw_transform(),
497 root_target_rect.size(),
498 root_target_rect,
499 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01500 false,
[email protected]f57bbc02012-11-21 07:02:15501 opacity);
502
[email protected]c1bb5af2013-03-13 19:06:27503 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17504
[email protected]c1bb5af2013-03-13 19:06:27505 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
506 bool did_invert = root_layer->screen_space_transform().GetInverse(
507 &transform_to_layer_space);
508 DCHECK(did_invert);
509 for (Region::Iterator fill_rects(fill_region);
510 fill_rects.has_rect();
511 fill_rects.next()) {
512 // The root layer transform is composed of translations and scales only,
513 // no perspective, so mapping is sufficient (as opposed to projecting).
514 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21515 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27516 // Skip the quad culler and just append the quads directly to avoid
517 // occlusion checks.
518 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]10a30b12013-05-02 16:42:11519 quad->SetNew(shared_quad_state, layer_rect, screen_background_color, false);
[email protected]c7e95b42013-03-18 01:13:49520 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27521 }
[email protected]467b3612012-08-28 07:41:16522}
523
[email protected]c1bb5af2013-03-13 19:06:27524bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
525 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14526
[email protected]c1bb5af2013-03-13 19:06:27527 if (!CanDraw() || !active_tree_->root_layer())
528 return false;
[email protected]2d692992012-12-19 01:19:32529
[email protected]c1bb5af2013-03-13 19:06:27530 TrackDamageForAllSurfaces(active_tree_->root_layer(),
531 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14532
[email protected]e0341352013-04-06 05:01:20533 // If the root render surface has no visible damage, then don't generate a
534 // frame at all.
535 RenderSurfaceImpl* root_surface =
536 active_tree_->root_layer()->render_surface();
537 bool root_surface_has_no_visible_damage =
538 !root_surface->damage_tracker()->current_damage_rect().Intersects(
539 root_surface->content_rect());
540 bool root_surface_has_contributing_layers =
541 !root_surface->layer_list().empty();
542 if (root_surface_has_contributing_layers &&
543 root_surface_has_no_visible_damage) {
544 TRACE_EVENT0("cc",
545 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
546 frame->has_no_damage = true;
547 return true;
548 }
549
[email protected]c1bb5af2013-03-13 19:06:27550 TRACE_EVENT1("cc",
551 "LayerTreeHostImpl::CalculateRenderPasses",
552 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06553 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14554
[email protected]c1bb5af2013-03-13 19:06:27555 // Create the render passes in dependency order.
556 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06557 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27558 --surface_index) {
559 LayerImpl* render_surface_layer =
560 (*frame->render_surface_layer_list)[surface_index];
561 render_surface_layer->render_surface()->AppendRenderPasses(frame);
562 }
[email protected]94f206c12012-08-25 00:09:14563
[email protected]c1bb5af2013-03-13 19:06:27564 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52565 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27566 base::debug::TraceLog::GetInstance() &&
567 base::debug::TraceLog::GetInstance()->IsEnabled();
568 OcclusionTrackerImpl occlusion_tracker(
569 active_tree_->root_layer()->render_surface()->content_rect(),
570 record_metrics_for_frame);
571 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52572 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14573
[email protected]846f455b2013-03-18 19:07:41574 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27575 occlusion_tracker.set_occluding_screen_space_rects_container(
576 &frame->occluding_screen_space_rects);
577 }
[email protected]846f455b2013-03-18 19:07:41578 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27579 occlusion_tracker.set_non_occluding_screen_space_rects_container(
580 &frame->non_occluding_screen_space_rects);
581 }
[email protected]94f206c12012-08-25 00:09:14582
[email protected]c1bb5af2013-03-13 19:06:27583 // Add quads to the Render passes in FrontToBack order to allow for testing
584 // occlusion and performing culling during the tree walk.
585 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28586 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27587 RenderSurfaceImpl,
588 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14589
[email protected]c1bb5af2013-03-13 19:06:27590 // Typically when we are missing a texture and use a checkerboard quad, we
591 // still draw the frame. However when the layer being checkerboarded is moving
592 // due to an impl-animation, we drop the frame to avoid flashing due to the
593 // texture suddenly appearing in the future.
594 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25595 // When we have a copy request for a layer, we need to draw no matter
596 // what, as the layer may disappear after this frame.
597 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14598
[email protected]372bad5f2013-03-21 16:38:43599 int layers_drawn = 0;
600
[email protected]ffbb2212013-06-02 23:47:59601 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
602
[email protected]c1bb5af2013-03-13 19:06:27603 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09604 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27605 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09606 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27607 it != end;
608 ++it) {
609 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09610 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27611 RenderPass* target_render_pass =
612 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14613
[email protected]0e5f7142013-05-24 06:45:36614 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23615 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14616
[email protected]c1bb5af2013-03-13 19:06:27617 AppendQuadsData append_quads_data(target_render_pass->id);
[email protected]89228202012-08-29 03:20:30618
[email protected]18a70192013-04-26 16:18:25619 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36620 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25621 have_copy_request = true;
[email protected]0e5f7142013-05-24 06:45:36622 it->TakeCopyRequests(&target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25623 }
624 } else if (it.represents_contributing_render_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27625 RenderPass::Id contributing_render_pass_id =
626 it->render_surface()->RenderPassId();
627 RenderPass* contributing_render_pass =
628 frame->render_passes_by_id[contributing_render_pass_id];
629 AppendQuadsForRenderSurfaceLayer(target_render_pass,
630 *it,
631 contributing_render_pass,
632 occlusion_tracker,
633 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09634 } else if (it.represents_itself() &&
635 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27636 bool has_occlusion_from_outside_target_surface;
637 bool impl_draw_transform_is_unknown = false;
638 if (occlusion_tracker.Occluded(
639 it->render_target(),
640 it->visible_content_rect(),
641 it->draw_transform(),
642 impl_draw_transform_is_unknown,
643 it->is_clipped(),
644 it->clip_rect(),
645 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09646 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27647 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59648 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27649 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59650
[email protected]c1bb5af2013-03-13 19:06:27651 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57652
[email protected]c1bb5af2013-03-13 19:06:27653 if (it->HasContributingDelegatedRenderPasses()) {
654 RenderPass::Id contributing_render_pass_id =
655 it->FirstContributingRenderPassId();
656 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
657 frame->render_passes_by_id.end()) {
658 RenderPass* render_pass =
659 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14660
[email protected]c1bb5af2013-03-13 19:06:27661 AppendQuadsData append_quads_data(render_pass->id);
662 AppendQuadsForLayer(render_pass,
663 *it,
664 occlusion_tracker,
665 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57666
[email protected]c1bb5af2013-03-13 19:06:27667 contributing_render_pass_id =
668 it->NextContributingRenderPassId(contributing_render_pass_id);
669 }
[email protected]94f206c12012-08-25 00:09:14670 }
671
[email protected]c1bb5af2013-03-13 19:06:27672 AppendQuadsForLayer(target_render_pass,
673 *it,
674 occlusion_tracker,
675 &append_quads_data);
676 }
[email protected]89228202012-08-29 03:20:30677
[email protected]372bad5f2013-03-21 16:38:43678 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14679 }
680
[email protected]e1e768f2013-03-26 08:48:09681 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27682 target_render_pass->has_occlusion_from_outside_target_surface = true;
683
[email protected]e1e768f2013-03-26 08:48:09684 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43685 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09686 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27687 bool layer_has_animating_transform =
688 it->screen_space_transform_is_animating() ||
689 it->draw_transform_is_animating();
690 if (layer_has_animating_transform)
691 draw_frame = false;
692 }
693
[email protected]e1e768f2013-03-26 08:48:09694 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27695 frame->contains_incomplete_tile = true;
696
697 occlusion_tracker.LeaveLayer(it);
698 }
699
[email protected]18a70192013-04-26 16:18:25700 if (have_copy_request)
701 draw_frame = true;
702
[email protected]372bad5f2013-03-21 16:38:43703 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
704
[email protected]1d993172012-10-18 18:15:04705#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27706 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
707 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
708 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
709 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
710 != frame->render_passes_by_id.end());
711 }
[email protected]94f206c12012-08-25 00:09:14712#endif
[email protected]c1bb5af2013-03-13 19:06:27713 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14714
[email protected]c1bb5af2013-03-13 19:06:27715 if (!active_tree_->has_transparent_background()) {
716 frame->render_passes.back()->has_transparent_background = false;
717 AppendQuadsToFillScreen(frame->render_passes.back(),
718 active_tree_->root_layer(),
719 active_tree_->background_color(),
720 occlusion_tracker);
721 }
[email protected]94f206c12012-08-25 00:09:14722
[email protected]c1bb5af2013-03-13 19:06:27723 if (draw_frame)
724 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25725 else
726 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14727
[email protected]c1bb5af2013-03-13 19:06:27728 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37729 if (!output_surface_->ForcedDrawToSoftwareDevice())
730 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]fbe89f72013-05-21 07:24:24731 if (renderer_) {
732 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
733 frame);
734 }
[email protected]94f206c12012-08-25 00:09:14735
[email protected]e0341352013-04-06 05:01:20736 // If we're making a frame to draw, it better have at least one render pass.
737 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27738 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14739}
740
[email protected]200a9c062013-05-20 04:34:37741void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
742 if (input_handler_client_)
743 input_handler_client_->MainThreadHasStoppedFlinging();
744}
745
[email protected]3d9f7432013-04-06 00:35:18746void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
747 bool should_background_tick) {
748 bool enabled = should_background_tick &&
749 !animation_registrar_->active_animation_controllers().empty();
750
[email protected]c1bb5af2013-03-13 19:06:27751 // Lazily create the time_source adapter so that we can vary the interval for
752 // testing.
753 if (!time_source_client_adapter_) {
754 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
755 this,
[email protected]6d0e69d2013-03-20 14:53:26756 DelayBasedTimeSource::Create(LowFrequencyAnimationInterval(),
[email protected]c1bb5af2013-03-13 19:06:27757 proxy_->CurrentThread()));
758 }
[email protected]94f206c12012-08-25 00:09:14759
[email protected]c1bb5af2013-03-13 19:06:27760 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14761}
762
[email protected]b9d4a362013-04-23 05:36:27763void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02764 viewport_damage_rect_.Union(damage_rect);
765}
766
[email protected]c1bb5af2013-03-13 19:06:27767static inline RenderPass* FindRenderPassById(
768 RenderPass::Id render_pass_id,
769 const LayerTreeHostImpl::FrameData& frame) {
770 RenderPassIdHashMap::const_iterator it =
771 frame.render_passes_by_id.find(render_pass_id);
772 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14773}
774
[email protected]c1bb5af2013-03-13 19:06:27775static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
776 LayerTreeHostImpl::FrameData* frame) {
777 RenderPass* remove_render_pass =
778 FindRenderPassById(remove_render_pass_id, *frame);
779 // The pass was already removed by another quad - probably the original, and
780 // we are the replica.
781 if (!remove_render_pass)
782 return;
783 RenderPassList& render_passes = frame->render_passes;
784 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
785 render_passes.end(),
786 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14787
[email protected]c1bb5af2013-03-13 19:06:27788 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14789
[email protected]c1bb5af2013-03-13 19:06:27790 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
791 frame->render_passes.erase(to_remove);
792 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14793
[email protected]c1bb5af2013-03-13 19:06:27794 // Now follow up for all RenderPass quads and remove their RenderPasses
795 // recursively.
796 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29797 QuadList::ConstBackToFrontIterator quad_list_iterator =
798 quad_list.BackToFrontBegin();
799 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27800 ++quad_list_iterator) {
801 DrawQuad* current_quad = (*quad_list_iterator);
802 if (current_quad->material != DrawQuad::RENDER_PASS)
803 continue;
[email protected]94f206c12012-08-25 00:09:14804
[email protected]c1bb5af2013-03-13 19:06:27805 RenderPass::Id next_remove_render_pass_id =
806 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
807 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
808 }
[email protected]94f206c12012-08-25 00:09:14809}
810
[email protected]c1bb5af2013-03-13 19:06:27811bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
812 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
813 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24814 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27815 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
816 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06817 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27818 if (quad_has_damage) {
819 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
820 return false;
821 } else if (!quad_has_cached_resource) {
822 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
823 return false;
824 }
825 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
826 return true;
[email protected]94f206c12012-08-25 00:09:14827}
828
[email protected]c1bb5af2013-03-13 19:06:27829bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
830 const RenderPassDrawQuad& quad, const FrameData& frame) const {
831 const RenderPass* render_pass =
832 FindRenderPassById(quad.render_pass_id, frame);
833 if (!render_pass)
834 return false;
[email protected]94f206c12012-08-25 00:09:14835
[email protected]c1bb5af2013-03-13 19:06:27836 // If any quad or RenderPass draws into this RenderPass, then keep it.
837 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29838 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
839 quad_list.BackToFrontBegin();
840 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27841 ++quad_list_iterator) {
842 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14843
[email protected]c1bb5af2013-03-13 19:06:27844 if (current_quad->material != DrawQuad::RENDER_PASS)
845 return false;
[email protected]94f206c12012-08-25 00:09:14846
[email protected]c1bb5af2013-03-13 19:06:27847 const RenderPass* contributing_pass = FindRenderPassById(
848 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
849 if (contributing_pass)
850 return false;
851 }
852 return true;
[email protected]94f206c12012-08-25 00:09:14853}
854
855// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27856template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
857 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
858 CullRenderPassesWithCachedTextures culler, FrameData* frame);
859template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
860 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
861 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14862
863// static
[email protected]c1bb5af2013-03-13 19:06:27864template <typename RenderPassCuller>
865void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
866 FrameData* frame) {
867 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
868 it != culler.RenderPassListEnd(frame->render_passes);
869 it = culler.RenderPassListNext(it)) {
870 const RenderPass* current_pass = frame->render_passes[it];
871 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29872 QuadList::ConstBackToFrontIterator quad_list_iterator =
873 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14874
[email protected]ed511b8d2013-03-25 03:29:29875 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27876 ++quad_list_iterator) {
877 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14878
[email protected]c1bb5af2013-03-13 19:06:27879 if (current_quad->material != DrawQuad::RENDER_PASS)
880 continue;
[email protected]94f206c12012-08-25 00:09:14881
[email protected]c1bb5af2013-03-13 19:06:27882 const RenderPassDrawQuad* render_pass_quad =
883 RenderPassDrawQuad::MaterialCast(current_quad);
884 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
885 continue;
[email protected]94f206c12012-08-25 00:09:14886
[email protected]c1bb5af2013-03-13 19:06:27887 // We are changing the vector in the middle of iteration. Because we
888 // delete render passes that draw into the current pass, we are
889 // guaranteed that any data from the iterator to the end will not
890 // change. So, capture the iterator position from the end of the
891 // list, and restore it after the change.
892 size_t position_from_end = frame->render_passes.size() - it;
893 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
894 it = frame->render_passes.size() - position_from_end;
895 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14896 }
[email protected]c1bb5af2013-03-13 19:06:27897 }
[email protected]94f206c12012-08-25 00:09:14898}
899
[email protected]e0341352013-04-06 05:01:20900bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
901 gfx::Rect device_viewport_damage_rect) {
[email protected]c1bb5af2013-03-13 19:06:27902 TRACE_EVENT0("cc", "LayerTreeHostImpl::PrepareToDraw");
[email protected]94f206c12012-08-25 00:09:14903
[email protected]7d19dc342013-05-02 22:02:04904 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:27905
[email protected]c1bb5af2013-03-13 19:06:27906 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
907 frame->render_passes.clear();
908 frame->render_passes_by_id.clear();
909 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:20910 frame->contains_incomplete_tile = false;
911 frame->has_no_damage = false;
912
913 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:02914 device_viewport_damage_rect.Union(viewport_damage_rect_);
915 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:20916
917 active_tree_->root_layer()->render_surface()->damage_tracker()->
918 AddDamageNextUpdate(device_viewport_damage_rect);
919 }
[email protected]94f206c12012-08-25 00:09:14920
[email protected]c1bb5af2013-03-13 19:06:27921 if (!CalculateRenderPasses(frame))
922 return false;
[email protected]94f206c12012-08-25 00:09:14923
[email protected]e4c3c87a2013-04-22 02:28:40924 frame->latency_info = active_tree_->GetLatencyInfo();
925
[email protected]c1bb5af2013-03-13 19:06:27926 // If we return true, then we expect DrawLayers() to be called before this
927 // function is called again.
928 return true;
[email protected]94f206c12012-08-25 00:09:14929}
930
[email protected]c1bb5af2013-03-13 19:06:27931void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
932 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:12933
[email protected]c1bb5af2013-03-13 19:06:27934 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:35935 visible_ ? policy.bytes_limit_when_visible
936 : policy.bytes_limit_when_not_visible,
937 ManagedMemoryPolicy::PriorityCutoffToValue(
938 visible_ ? policy.priority_cutoff_when_visible
939 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:27940 if (evicted_resources) {
941 active_tree_->SetContentsTexturesPurged();
942 if (pending_tree_)
943 pending_tree_->SetContentsTexturesPurged();
944 client_->SetNeedsCommitOnImplThread();
945 client_->OnCanDrawStateChanged(CanDraw());
946 client_->RenewTreePriority();
947 }
948 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:43949
[email protected]8be1a9bf2013-05-01 03:45:19950 UpdateTileManagerMemoryPolicy(policy);
951}
952
953void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
954 const ManagedMemoryPolicy& policy) {
955 if (!tile_manager_)
956 return;
957
958 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
959 new_state.memory_limit_in_bytes = visible_ ?
960 policy.bytes_limit_when_visible :
961 policy.bytes_limit_when_not_visible;
962 // TODO(reveman): We should avoid keeping around unused resources if
963 // possible. crbug.com/224475
964 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
965 (static_cast<int64>(new_state.memory_limit_in_bytes) *
966 settings_.max_unused_resource_memory_percentage) / 100);
967 new_state.memory_limit_policy =
968 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
969 visible_ ?
970 policy.priority_cutoff_when_visible :
971 policy.priority_cutoff_when_not_visible);
972 tile_manager_->SetGlobalState(new_state);
[email protected]94f206c12012-08-25 00:09:14973}
974
[email protected]c1bb5af2013-03-13 19:06:27975bool LayerTreeHostImpl::HasImplThread() const {
976 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:44977}
978
[email protected]c1bb5af2013-03-13 19:06:27979void LayerTreeHostImpl::ScheduleManageTiles() {
980 if (client_)
981 client_->SetNeedsManageTilesOnImplThread();
[email protected]8947cbe2012-11-28 05:27:43982}
983
[email protected]86126792013-03-16 20:07:54984void LayerTreeHostImpl::DidInitializeVisibleTile() {
985 // TODO(reveman): Determine tiles that changed and only damage
986 // what's necessary.
987 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:27988 if (client_)
[email protected]86126792013-03-16 20:07:54989 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:47990}
991
[email protected]fcb846d2013-05-22 01:42:36992bool LayerTreeHostImpl::
993 ShouldForceTileUploadsRequiredForActivationToComplete() const {
994 // During shutdown of TileManager, it will attempt to flush its job queue,
995 // which can call this function while this is NULL.
996 if (!tile_manager_)
997 return false;
998
999 TreePriority tree_priority = tile_manager_->GlobalState().tree_priority;
1000 return tree_priority != SMOOTHNESS_TAKES_PRIORITY &&
1001 animation_registrar_->active_animation_controllers().empty();
1002}
1003
[email protected]c1bb5af2013-03-13 19:06:271004bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:521005 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:521006}
1007
[email protected]c1bb5af2013-03-13 19:06:271008void LayerTreeHostImpl::SetManagedMemoryPolicy(
1009 const ManagedMemoryPolicy& policy) {
1010 if (managed_memory_policy_ == policy)
1011 return;
[email protected]61de5812012-11-08 07:03:441012
[email protected]d7626ffd2013-03-29 00:17:421013 // If there is already enough memory to draw everything imaginable and the
1014 // new memory limit does not change this, then do not re-commit. Don't bother
1015 // skipping commits if this is not visible (commits don't happen when not
1016 // visible, there will almost always be a commit when this becomes visible).
1017 bool needs_commit = true;
1018 if (visible() &&
1019 policy.bytes_limit_when_visible >=
1020 max_memory_needed_bytes_ &&
1021 managed_memory_policy_.bytes_limit_when_visible >=
1022 max_memory_needed_bytes_ &&
1023 policy.priority_cutoff_when_visible ==
1024 managed_memory_policy_.priority_cutoff_when_visible) {
1025 needs_commit = false;
1026 }
1027
[email protected]c1bb5af2013-03-13 19:06:271028 managed_memory_policy_ = policy;
1029 if (!proxy_->HasImplThread()) {
[email protected]d7626ffd2013-03-29 00:17:421030 // In single-thread mode, this can be called on the main thread by
1031 // GLRenderer::OnMemoryAllocationChanged.
[email protected]c1bb5af2013-03-13 19:06:271032 DebugScopedSetImplThread impl_thread(proxy_);
[email protected]206a3922013-05-17 06:34:121033 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271034 } else {
1035 DCHECK(proxy_->IsImplThread());
[email protected]206a3922013-05-17 06:34:121036 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271037 }
[email protected]d7626ffd2013-03-29 00:17:421038
1039 if (needs_commit)
1040 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141041}
1042
[email protected]f224cc92013-06-06 23:23:321043void LayerTreeHostImpl::SetExternalDrawConstraints(
1044 const gfx::Transform& transform,
1045 gfx::Rect viewport) {
1046 external_transform_ = transform;
1047 external_viewport_ = viewport;
1048}
1049
[email protected]1cd9f5552013-04-26 04:22:031050void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1051 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1052}
1053
[email protected]c1bb5af2013-03-13 19:06:271054void LayerTreeHostImpl::OnVSyncParametersChanged(base::TimeTicks timebase,
1055 base::TimeDelta interval) {
1056 client_->OnVSyncParametersChanged(timebase, interval);
[email protected]94f206c12012-08-25 00:09:141057}
1058
[email protected]c8cbae72013-05-23 10:45:031059void LayerTreeHostImpl::BeginFrame(base::TimeTicks frame_time) {
1060 client_->BeginFrameOnImplThread(frame_time);
[email protected]7ed47512013-03-26 22:28:451061}
1062
[email protected]c1bb5af2013-03-13 19:06:271063void LayerTreeHostImpl::OnSendFrameToParentCompositorAck(
1064 const CompositorFrameAck& ack) {
1065 if (!renderer_)
1066 return;
[email protected]b6f3d7e2012-12-08 00:11:211067
[email protected]c1bb5af2013-03-13 19:06:271068 // TODO(piman): We may need to do some validation on this ack before
1069 // processing it.
1070 renderer_->ReceiveCompositorFrameAck(ack);
[email protected]bb1e2822013-04-17 22:06:011071
1072 // When using compositor frame data, the ack doubles as a swap complete ack.
1073 OnSwapBuffersComplete();
[email protected]a46f32932012-12-07 21:43:161074}
1075
[email protected]c1bb5af2013-03-13 19:06:271076void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1077 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291078}
1079
[email protected]c1bb5af2013-03-13 19:06:271080CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1081 CompositorFrameMetadata metadata;
1082 metadata.device_scale_factor = device_scale_factor_;
1083 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1084 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1085 metadata.root_layer_size = active_tree_->ScrollableSize();
1086 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1087 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1088 if (top_controls_manager_) {
1089 metadata.location_bar_offset =
1090 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1091 metadata.location_bar_content_translation =
1092 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011093 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271094 }
[email protected]bf189f62012-12-18 03:42:111095
[email protected]c1bb5af2013-03-13 19:06:271096 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111097 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271098
[email protected]ffb2720f2013-03-15 19:18:371099 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]e4c3c87a2013-04-22 02:28:401100 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271101
1102 return metadata;
[email protected]bf189f62012-12-18 03:42:111103}
1104
[email protected]73673592013-04-03 22:14:321105bool LayerTreeHostImpl::AllowPartialSwap() const {
1106 // We don't track damage on the HUD layer (it interacts with damage tracking
1107 // visualizations), so disable partial swaps to make the HUD layer display
1108 // properly.
1109 return !debug_state_.ShowHudRects();
1110}
1111
[email protected]ff1211d2013-06-07 01:58:351112class DidBeginTracingFunctor {
1113 public:
1114 void operator()(LayerImpl* layer) {
1115 layer->DidBeginTracing();
1116 }
1117};
[email protected]37349bc2013-06-04 01:31:521118
[email protected]f0c2a242013-03-15 19:34:521119void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1120 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:271121 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
1122 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201123
1124 if (frame->has_no_damage)
1125 return;
1126
[email protected]c1bb5af2013-03-13 19:06:271127 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141128
[email protected]9e3594522013-03-18 00:57:361129 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:141130
[email protected]372bad5f2013-03-21 16:38:431131 rendering_stats_instrumentation_->SetScreenFrameCount(
1132 fps_counter_->current_frame_number());
1133 rendering_stats_instrumentation_->SetDroppedFrameCount(
1134 fps_counter_->dropped_frame_count());
1135
[email protected]c1bb5af2013-03-13 19:06:271136 if (tile_manager_) {
1137 memory_history_->SaveEntry(
1138 tile_manager_->memory_stats_from_last_assign());
1139 }
[email protected]1191d9d2013-02-02 06:00:331140
[email protected]846f455b2013-03-18 19:07:411141 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021142 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271143 active_tree_->root_layer(),
1144 *frame->render_surface_layer_list,
1145 frame->occluding_screen_space_rects,
1146 frame->non_occluding_screen_space_rects,
1147 debug_state_);
1148 }
[email protected]94f206c12012-08-25 00:09:141149
[email protected]a848c102013-03-26 08:59:091150 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1151 const RenderingStats& stats =
1152 rendering_stats_instrumentation_->GetRenderingStats();
1153 paint_time_counter_->SavePaintTime(stats.total_paint_time);
1154 }
1155
[email protected]37349bc2013-06-04 01:31:521156 bool is_new_trace;
1157 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1158 if (is_new_trace) {
[email protected]ff1211d2013-06-07 01:58:351159 if (pending_tree_)
1160 LayerTreeHostCommon::CallFunctionForSubtree<
1161 DidBeginTracingFunctor, LayerImpl>(
1162 pending_tree_->root_layer());
1163 LayerTreeHostCommon::CallFunctionForSubtree<
1164 DidBeginTracingFunctor, LayerImpl>(
1165 active_tree_->root_layer());
[email protected]37349bc2013-06-04 01:31:521166 }
1167
[email protected]9197dbcb2013-05-15 20:28:511168 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1169 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this,
1170 TracedValue::FromValue(AsValue().release()));
[email protected]131a0c22013-02-12 18:31:081171
[email protected]c1bb5af2013-03-13 19:06:271172 // Because the contents of the HUD depend on everything else in the frame, the
1173 // contents of its texture are updated as the last thing before the frame is
1174 // drawn.
1175 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541176 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141177
[email protected]b09c1942013-05-10 00:06:371178 if (output_surface_->ForcedDrawToSoftwareDevice()) {
1179 scoped_ptr<SoftwareRenderer> temp_software_renderer =
1180 SoftwareRenderer::Create(this, output_surface_.get(), NULL);
1181 temp_software_renderer->DrawFrame(&frame->render_passes);
1182 } else {
1183 renderer_->DrawFrame(&frame->render_passes);
1184 }
[email protected]c1bb5af2013-03-13 19:06:271185 // The render passes should be consumed by the renderer.
1186 DCHECK(frame->render_passes.empty());
1187 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141188
[email protected]c1bb5af2013-03-13 19:06:271189 // The next frame should start by assuming nothing has changed, and changes
1190 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541191 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271192 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1193 DidDrawDamagedArea();
1194 }
1195 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]94f206c12012-08-25 00:09:141196}
1197
[email protected]c1bb5af2013-03-13 19:06:271198void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1199 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1200 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521201
[email protected]c1bb5af2013-03-13 19:06:271202 // Once all layers have been drawn, pending texture uploads should no
1203 // longer block future uploads.
[email protected]fbe89f72013-05-21 07:24:241204 if (resource_provider_)
1205 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141206}
1207
[email protected]c1bb5af2013-03-13 19:06:271208void LayerTreeHostImpl::FinishAllRendering() {
1209 if (renderer_)
1210 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141211}
1212
[email protected]c1bb5af2013-03-13 19:06:271213bool LayerTreeHostImpl::IsContextLost() {
1214 DCHECK(proxy_->IsImplThread());
1215 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141216}
1217
[email protected]c1bb5af2013-03-13 19:06:271218const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1219 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141220}
1221
[email protected]e0341352013-04-06 05:01:201222bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1223 if (frame.has_no_damage)
1224 return false;
[email protected]c5c506382013-04-30 04:42:161225 renderer_->SwapBuffers(frame.latency_info);
1226 active_tree_->ClearLatencyInfo();
1227 return true;
[email protected]94f206c12012-08-25 00:09:141228}
1229
[email protected]c8cbae72013-05-23 10:45:031230void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451231 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031232 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451233}
1234
[email protected]94c40e6292013-05-24 22:01:501235float LayerTreeHostImpl::DeviceScaleFactor() const {
1236 return device_scale_factor_;
1237}
1238
[email protected]ffb2720f2013-03-15 19:18:371239gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
1240 gfx::SizeF dip_size =
[email protected]f224cc92013-06-06 23:23:321241 gfx::ScaleSize(device_viewport_size(), 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371242
1243 // The clip layer should be used if non-overlay scrollbars may exist since
1244 // it adjusts for them.
1245 LayerImpl* clip_layer = active_tree_->RootClipLayer();
[email protected]8e0176d2013-03-21 03:14:521246 if (!Settings().solid_color_scrollbars && clip_layer &&
[email protected]ffb2720f2013-03-15 19:18:371247 clip_layer->masks_to_bounds())
1248 dip_size = clip_layer->bounds();
1249
[email protected]ed511b8d2013-03-25 03:29:291250 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371251 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401252 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291253 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371254}
1255
[email protected]c1bb5af2013-03-13 19:06:271256const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1257 return settings();
[email protected]493067512012-09-19 23:34:101258}
1259
[email protected]c1bb5af2013-03-13 19:06:271260void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011261 // TODO(jamesr): The renderer_ check is needed to make some of the
1262 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1263 // important) in production. We should adjust the test to not need this.
1264 if (renderer_)
1265 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141266}
1267
[email protected]c1bb5af2013-03-13 19:06:271268void LayerTreeHostImpl::OnSwapBuffersComplete() {
1269 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]94f206c12012-08-25 00:09:141270}
1271
[email protected]c1bb5af2013-03-13 19:06:271272void LayerTreeHostImpl::Readback(void* pixels,
1273 gfx::Rect rect_in_device_viewport) {
1274 DCHECK(renderer_);
1275 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141276}
1277
[email protected]59adb112013-04-09 04:48:441278bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341279 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011280}
1281
[email protected]c1bb5af2013-03-13 19:06:271282LayerImpl* LayerTreeHostImpl::RootLayer() const {
1283 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081284}
1285
[email protected]c1bb5af2013-03-13 19:06:271286LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1287 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081288}
1289
[email protected]c1bb5af2013-03-13 19:06:271290LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1291 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081292}
1293
[email protected]94f206c12012-08-25 00:09:141294// Content layers can be either directly scrollable or contained in an outer
1295// scrolling layer which applies the scroll transform. Given a content layer,
1296// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271297static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1298 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141299 return 0;
[email protected]c1bb5af2013-03-13 19:06:271300
1301 if (layer_impl->scrollable())
1302 return layer_impl;
1303
1304 if (layer_impl->DrawsContent() &&
1305 layer_impl->parent() &&
1306 layer_impl->parent()->scrollable())
1307 return layer_impl->parent();
1308
1309 return 0;
[email protected]94f206c12012-08-25 00:09:141310}
1311
[email protected]c1bb5af2013-03-13 19:06:271312void LayerTreeHostImpl::CreatePendingTree() {
1313 CHECK(!pending_tree_);
1314 if (recycle_tree_)
1315 recycle_tree_.swap(pending_tree_);
1316 else
1317 pending_tree_ = LayerTreeImpl::create(this);
1318 client_->OnCanDrawStateChanged(CanDraw());
1319 client_->OnHasPendingTreeStateChanged(pending_tree_);
1320 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1321 TRACE_EVENT_ASYNC_STEP0("cc",
1322 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271323}
1324
[email protected]c1bb5af2013-03-13 19:06:271325void LayerTreeHostImpl::CheckForCompletedTileUploads() {
1326 DCHECK(!client_->IsInsideDraw()) <<
1327 "Checking for completed uploads within a draw may trigger "
1328 "spurious redraws.";
1329 if (tile_manager_)
1330 tile_manager_->CheckForCompletedTileUploads();
[email protected]eabe5002013-01-12 22:07:481331}
1332
[email protected]c1bb5af2013-03-13 19:06:271333bool LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1334 if (!pending_tree_)
1335 return false;
[email protected]2e7ca422012-12-20 02:57:271336
[email protected]fbe89f72013-05-21 07:24:241337 CHECK(settings_.impl_side_painting);
[email protected]2ae038b2013-01-28 12:52:091338
[email protected]fcb846d2013-05-22 01:42:361339 // TODO(enne): This needs to be moved somewhere else (post-animate?)
[email protected]7d19dc342013-05-02 22:02:041340 pending_tree_->UpdateDrawProperties();
[email protected]615c78a2013-01-24 23:44:161341
[email protected]f6742f52013-05-08 23:52:221342 TRACE_EVENT_ASYNC_STEP1(
[email protected]fcb846d2013-05-22 01:42:361343 "cc",
1344 "PendingTree", pending_tree_.get(), "activate",
1345 "state", TracedValue::FromValue(ActivationStateAsValue().release()));
[email protected]4f0a5002013-01-28 13:02:271346
[email protected]fcb846d2013-05-22 01:42:361347 if (tile_manager_) {
1348 tile_manager_->CheckForCompletedTileUploads();
1349 if (!tile_manager_->AreTilesRequiredForActivationReady())
[email protected]fbe89f72013-05-21 07:24:241350 return false;
1351 }
[email protected]2e7ca422012-12-20 02:57:271352
[email protected]c1bb5af2013-03-13 19:06:271353 ActivatePendingTree();
1354 return true;
[email protected]2e7ca422012-12-20 02:57:271355}
1356
[email protected]c1bb5af2013-03-13 19:06:271357void LayerTreeHostImpl::ActivatePendingTree() {
1358 CHECK(pending_tree_);
1359 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351360
[email protected]1960a712013-04-30 17:06:471361 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271362 active_tree_->PushPersistedState(pending_tree_.get());
1363 if (pending_tree_->needs_full_tree_sync()) {
1364 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331365 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271366 active_tree_->DetachLayerTree(),
1367 active_tree_.get()));
1368 }
[email protected]b5651c22013-03-14 15:06:331369 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271370 active_tree_->root_layer());
1371 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511372
[email protected]c1bb5af2013-03-13 19:06:271373 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511374
[email protected]c1bb5af2013-03-13 19:06:271375 // Now that we've synced everything from the pending tree to the active
1376 // tree, rename the pending tree the recycle tree so we can reuse it on the
1377 // next sync.
1378 pending_tree_.swap(recycle_tree_);
1379 recycle_tree_->ClearRenderSurfaces();
[email protected]48871fc2013-01-23 07:36:511380
[email protected]1960a712013-04-30 17:06:471381 active_tree_->SetRootLayerScrollOffsetDelegate(
1382 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271383 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221384
[email protected]c1bb5af2013-03-13 19:06:271385 // Reduce wasted memory now that unlinked resources are guaranteed not
1386 // to be used.
1387 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411388
[email protected]c1bb5af2013-03-13 19:06:271389 client_->OnCanDrawStateChanged(CanDraw());
1390 client_->OnHasPendingTreeStateChanged(pending_tree_);
1391 client_->SetNeedsRedrawOnImplThread();
1392 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241393
[email protected]a848c102013-03-26 08:59:091394 if (debug_state_.continuous_painting) {
1395 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431396 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091397 paint_time_counter_->SavePaintTime(
[email protected]41abb6d92013-04-26 16:38:501398 stats.total_paint_time + stats.total_record_time +
[email protected]a848c102013-03-26 08:59:091399 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271400 }
[email protected]2a61ad52013-05-13 14:01:291401
1402 client_->DidActivatePendingTree();
[email protected]2e7ca422012-12-20 02:57:271403}
1404
[email protected]c1bb5af2013-03-13 19:06:271405void LayerTreeHostImpl::SetVisible(bool visible) {
1406 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141407
[email protected]c1bb5af2013-03-13 19:06:271408 if (visible_ == visible)
1409 return;
1410 visible_ = visible;
1411 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121412 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141413
[email protected]c1bb5af2013-03-13 19:06:271414 if (!renderer_)
1415 return;
[email protected]94f206c12012-08-25 00:09:141416
[email protected]c1bb5af2013-03-13 19:06:271417 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141418}
1419
[email protected]206a3922013-05-17 06:34:121420ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
1421 if (!debug_state_.rasterize_only_visible_content)
1422 return managed_memory_policy_;
1423
1424 ManagedMemoryPolicy actual = managed_memory_policy_;
1425 actual.priority_cutoff_when_not_visible =
1426 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1427 actual.priority_cutoff_when_visible =
1428 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1429 return actual;
1430}
1431
[email protected]c1bb5af2013-03-13 19:06:271432bool LayerTreeHostImpl::InitializeRenderer(
1433 scoped_ptr<OutputSurface> output_surface) {
1434 // Since we will create a new resource provider, we cannot continue to use
1435 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1436 // before we destroy the old resource provider.
1437 if (active_tree_->root_layer())
1438 ClearRenderSurfaces();
[email protected]ff1211d2013-06-07 01:58:351439 if (active_tree_->root_layer())
1440 SendDidLoseOutputSurfaceRecursive(active_tree_->root_layer());
1441 if (pending_tree_ && pending_tree_->root_layer())
1442 SendDidLoseOutputSurfaceRecursive(pending_tree_->root_layer());
1443 if (recycle_tree_ && recycle_tree_->root_layer())
1444 SendDidLoseOutputSurfaceRecursive(recycle_tree_->root_layer());
[email protected]7ba3ca72013-04-11 06:37:251445 if (resource_provider_)
1446 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401447
[email protected]c1bb5af2013-03-13 19:06:271448 // Note: order is important here.
1449 renderer_.reset();
1450 tile_manager_.reset();
1451 resource_provider_.reset();
1452 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141453
[email protected]c1bb5af2013-03-13 19:06:271454 if (!output_surface->BindToClient(this))
1455 return false;
[email protected]be3181652012-09-25 13:02:131456
[email protected]2b154b22013-06-07 09:03:271457 return DoInitializeRenderer(output_surface.Pass(),
1458 false /* is_deferred_init */);
1459}
1460
1461bool LayerTreeHostImpl::DoInitializeRenderer(
1462 scoped_ptr<OutputSurface> output_surface,
1463 bool is_deferred_init) {
1464 if (output_surface->capabilities().deferred_gl_initialization &&
1465 !is_deferred_init) {
[email protected]fbe89f72013-05-21 07:24:241466 // TODO(joth): Defer creating the Renderer too, until GL is initialized.
1467 // See http://crbug.com/230197
1468 renderer_ = SoftwareRenderer::Create(this, output_surface.get(), NULL);
1469 } else {
1470 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1471 output_surface.get(), settings_.highp_threshold_min);
1472 if (!resource_provider)
1473 return false;
[email protected]be3181652012-09-25 13:02:131474
[email protected]fbe89f72013-05-21 07:24:241475 if (output_surface->capabilities().has_parent_compositor) {
1476 renderer_ = DelegatingRenderer::Create(this, output_surface.get(),
1477 resource_provider.get());
1478 } else if (output_surface->context3d()) {
1479 renderer_ = GLRenderer::Create(this,
1480 output_surface.get(),
1481 resource_provider.get(),
[email protected]bec084292013-05-21 21:31:441482 settings_.highp_threshold_min,
1483 settings_.force_direct_layer_drawing);
[email protected]fbe89f72013-05-21 07:24:241484 } else if (output_surface->software_device()) {
1485 renderer_ = SoftwareRenderer::Create(this,
1486 output_surface.get(),
[email protected]c1bb5af2013-03-13 19:06:271487 resource_provider.get());
[email protected]fbe89f72013-05-21 07:24:241488 }
1489 if (!renderer_)
1490 return false;
[email protected]be3181652012-09-25 13:02:131491
[email protected]6123930c2013-05-23 06:48:371492 if (settings_.impl_side_painting) {
1493 bool using_map_image = GetRendererCapabilities().using_map_image;
1494 tile_manager_ = TileManager::Create(this,
1495 resource_provider.get(),
1496 settings_.num_raster_threads,
1497 settings_.use_color_estimator,
1498 rendering_stats_instrumentation_,
1499 using_map_image);
1500 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
1501 }
1502
[email protected]fbe89f72013-05-21 07:24:241503 resource_provider_ = resource_provider.Pass();
1504 }
1505
[email protected]c1bb5af2013-03-13 19:06:271506 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141507
[email protected]c1bb5af2013-03-13 19:06:271508 if (!visible_)
1509 renderer_->SetVisible(visible_);
[email protected]94f206c12012-08-25 00:09:141510
[email protected]c1bb5af2013-03-13 19:06:271511 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211512
[email protected]c1bb5af2013-03-13 19:06:271513 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1514 // to be initialized to get max texture size.
1515 active_tree_->set_needs_update_draw_properties();
1516 if (pending_tree_)
1517 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161518
[email protected]c1bb5af2013-03-13 19:06:271519 return true;
[email protected]94f206c12012-08-25 00:09:141520}
1521
[email protected]2b154b22013-06-07 09:03:271522bool LayerTreeHostImpl::DeferredInitialize(
1523 scoped_refptr<ContextProvider> offscreen_context_provider) {
1524 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1525 DCHECK(output_surface_->context3d());
1526
1527 // TODO(boliu): This is temporary until proper resource clean up is possible
1528 // without resetting |tile_manager_| or |resource_provider_|.
1529 DCHECK(!resource_provider_);
1530
1531 bool success =
1532 DoInitializeRenderer(output_surface_.Pass(), true /* is_deferred_init */);
1533 client_->DidTryInitializeRendererOnImplThread(success,
1534 offscreen_context_provider);
1535 return success;
1536}
1537
[email protected]18ce59702013-04-09 04:58:401538void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1539 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271540 return;
[email protected]94f206c12012-08-25 00:09:141541
[email protected]c1bb5af2013-03-13 19:06:271542 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1543 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271544
[email protected]c1bb5af2013-03-13 19:06:271545 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141546
[email protected]c1bb5af2013-03-13 19:06:271547 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141548
[email protected]c1bb5af2013-03-13 19:06:271549 if (renderer_)
1550 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211551
[email protected]c1bb5af2013-03-13 19:06:271552 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441553 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141554}
1555
[email protected]d9083762013-03-24 01:36:401556void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1557 if (overdraw_bottom_height == overdraw_bottom_height_)
1558 return;
1559 overdraw_bottom_height_ = overdraw_bottom_height;
1560
1561 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441562 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401563}
1564
[email protected]c1bb5af2013-03-13 19:06:271565void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1566 if (device_scale_factor == device_scale_factor_)
1567 return;
1568 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271569
[email protected]94c40e6292013-05-24 22:01:501570 if (renderer_)
1571 renderer_->ViewportChanged();
1572
[email protected]c1bb5af2013-03-13 19:06:271573 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441574 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141575}
1576
[email protected]f224cc92013-06-06 23:23:321577gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1578 if (external_viewport_.IsEmpty())
1579 return gfx::Rect(device_viewport_size_);
1580
1581 return external_viewport_;
1582}
1583
1584const gfx::Transform& LayerTreeHostImpl::DeviceTransform() const {
1585 return external_transform_;
1586}
1587
[email protected]c1bb5af2013-03-13 19:06:271588void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1589 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141590}
1591
[email protected]59adb112013-04-09 04:48:441592void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271593 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441594 active_tree_->set_needs_update_draw_properties();
1595 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141596}
1597
[email protected]c1bb5af2013-03-13 19:06:271598bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041599 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341600 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141601}
1602
[email protected]200a9c062013-05-20 04:34:371603void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1604 DCHECK(input_handler_client_ == NULL);
1605 input_handler_client_ = client;
1606}
1607
[email protected]5ff3c9782013-04-29 17:35:121608InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1609 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291610 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141611
[email protected]c1bb5af2013-03-13 19:06:271612 if (top_controls_manager_)
1613 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381614
[email protected]c1bb5af2013-03-13 19:06:271615 DCHECK(!CurrentlyScrollingLayer());
1616 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141617
[email protected]c1bb5af2013-03-13 19:06:271618 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141619 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141620
[email protected]c1bb5af2013-03-13 19:06:271621 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1622 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141623
[email protected]c1bb5af2013-03-13 19:06:271624 // First find out which layer was hit from the saved list of visible layers
1625 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391626 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271627 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521628
[email protected]c1bb5af2013-03-13 19:06:271629 // Walk up the hierarchy and look for a scrollable layer.
1630 LayerImpl* potentially_scrolling_layer_impl = 0;
1631 for (; layer_impl; layer_impl = layer_impl->parent()) {
1632 // The content layer can also block attempts to scroll outside the main
1633 // thread.
1634 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1635 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431636 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271637 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1638 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141639 }
1640
[email protected]c1bb5af2013-03-13 19:06:271641 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1642 if (!scroll_layer_impl)
1643 continue;
[email protected]94f206c12012-08-25 00:09:141644
[email protected]c1bb5af2013-03-13 19:06:271645 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141646
[email protected]c1bb5af2013-03-13 19:06:271647 // If any layer wants to divert the scroll event to the main thread, abort.
1648 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431649 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271650 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1651 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141652 }
1653
[email protected]c1bb5af2013-03-13 19:06:271654 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1655 potentially_scrolling_layer_impl = scroll_layer_impl;
1656 }
1657
1658 // When hiding top controls is enabled and the controls are hidden or
1659 // overlaying the content, force scrolls to be enabled on the root layer to
1660 // allow bringing the top controls back into view.
1661 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1662 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521663 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271664 potentially_scrolling_layer_impl = RootScrollLayer();
1665 }
1666
1667 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201668 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271669 potentially_scrolling_layer_impl);
1670 should_bubble_scrolls_ = (type != NonBubblingGesture);
1671 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431672 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271673 client_->RenewTreePriority();
1674 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
1675 return ScrollStarted;
1676 }
1677 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141678}
1679
[email protected]c1bb5af2013-03-13 19:06:271680gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1681 LayerImpl* layer_impl,
1682 float scale_from_viewport_to_screen_space,
1683 gfx::PointF viewport_point,
1684 gfx::Vector2dF viewport_delta) {
1685 // Layers with non-invertible screen space transforms should not have passed
1686 // the scroll hit test in the first place.
1687 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1688 gfx::Transform inverse_screen_space_transform(
1689 gfx::Transform::kSkipInitialization);
1690 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1691 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351692 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1693 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271694 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141695
[email protected]c1bb5af2013-03-13 19:06:271696 gfx::PointF screen_space_point =
1697 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141698
[email protected]c1bb5af2013-03-13 19:06:271699 gfx::Vector2dF screen_space_delta = viewport_delta;
1700 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1701
1702 // First project the scroll start and end points to local layer space to find
1703 // the scroll delta in layer coordinates.
1704 bool start_clipped, end_clipped;
1705 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1706 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211707 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271708 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211709 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271710 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211711 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271712 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211713 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271714
1715 // In general scroll point coordinates should not get clipped.
1716 DCHECK(!start_clipped);
1717 DCHECK(!end_clipped);
1718 if (start_clipped || end_clipped)
1719 return gfx::Vector2dF();
1720
1721 // local_start_point and local_end_point are in content space but we want to
1722 // move them to layer space for scrolling.
1723 float width_scale = 1.f / layer_impl->contents_scale_x();
1724 float height_scale = 1.f / layer_impl->contents_scale_y();
1725 local_start_point.Scale(width_scale, height_scale);
1726 local_end_point.Scale(width_scale, height_scale);
1727
1728 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:471729 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:271730 layer_impl->ScrollBy(local_end_point - local_start_point);
1731
1732 // Get the end point in the layer's content space so we can apply its
1733 // ScreenSpaceTransform.
1734 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:471735 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:271736 previous_delta;
1737 gfx::PointF actual_local_content_end_point =
1738 gfx::ScalePoint(actual_local_end_point,
1739 1.f / width_scale,
1740 1.f / height_scale);
1741
1742 // Calculate the applied scroll delta in viewport space coordinates.
1743 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211744 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271745 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211746 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271747 DCHECK(!end_clipped);
1748 if (end_clipped)
1749 return gfx::Vector2dF();
1750 gfx::PointF actual_viewport_end_point =
1751 gfx::ScalePoint(actual_screen_space_end_point,
1752 1.f / scale_from_viewport_to_screen_space);
1753 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141754}
1755
[email protected]c1bb5af2013-03-13 19:06:271756static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1757 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:471758 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271759 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:471760 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:271761}
1762
1763bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1764 gfx::Vector2dF scroll_delta) {
1765 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1766 if (!CurrentlyScrollingLayer())
1767 return false;
1768
1769 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:531770 gfx::Vector2dF unused_root_delta;
[email protected]c1bb5af2013-03-13 19:06:271771 bool did_scroll = false;
[email protected]60b4d252013-03-23 18:49:421772 bool consume_by_top_controls = top_controls_manager_ &&
1773 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061774
[email protected]c1bb5af2013-03-13 19:06:271775 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1776 layer_impl;
1777 layer_impl = layer_impl->parent()) {
1778 if (!layer_impl->scrollable())
1779 continue;
1780
[email protected]a2b5ded2013-05-20 21:32:531781 if (layer_impl == RootScrollLayer()) {
1782 // Only allow bubble scrolling when the scroll is in the direction to make
1783 // the top controls visible.
1784 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1785 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1786 UpdateMaxScrollOffset();
1787 }
1788 // Track root layer deltas for reporting overscroll.
1789 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:421790 }
1791
[email protected]c1bb5af2013-03-13 19:06:271792 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271793 // Gesture events need to be transformed from viewport coordinates to local
1794 // layer coordinates so that the scrolling contents exactly follow the
1795 // user's finger. In contrast, wheel events represent a fixed amount of
1796 // scrolling so we can just apply them directly.
1797 if (!wheel_scrolling_) {
1798 float scale_from_viewport_to_screen_space = device_scale_factor_;
1799 applied_delta =
1800 ScrollLayerWithViewportSpaceDelta(layer_impl,
1801 scale_from_viewport_to_screen_space,
1802 viewport_point, pending_delta);
1803 } else {
1804 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141805 }
[email protected]94f206c12012-08-25 00:09:141806
[email protected]c1bb5af2013-03-13 19:06:271807 // If the layer wasn't able to move, try the next one in the hierarchy.
1808 float move_threshold_squared = 0.1f * 0.1f;
1809 if (applied_delta.LengthSquared() < move_threshold_squared) {
1810 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1811 continue;
1812 else
1813 break;
[email protected]94f206c12012-08-25 00:09:141814 }
[email protected]a2b5ded2013-05-20 21:32:531815
1816 if (layer_impl == RootScrollLayer())
1817 unused_root_delta.Subtract(applied_delta);
1818
[email protected]c1bb5af2013-03-13 19:06:271819 did_scroll = true;
1820 did_lock_scrolling_layer_ = true;
1821 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:201822 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:271823 break;
[email protected]94f206c12012-08-25 00:09:141824 }
[email protected]94f206c12012-08-25 00:09:141825
[email protected]c1bb5af2013-03-13 19:06:271826 // If the applied delta is within 45 degrees of the input delta, bail out to
1827 // make it easier to scroll just one layer in one direction without
1828 // affecting any of its parents.
1829 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:211830 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:271831 applied_delta, pending_delta) < angle_threshold) {
1832 pending_delta = gfx::Vector2d();
1833 break;
[email protected]4a23c374c2012-12-08 08:38:551834 }
[email protected]c1bb5af2013-03-13 19:06:271835
1836 // Allow further movement only on an axis perpendicular to the direction in
1837 // which the layer moved.
1838 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:211839 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:271840
[email protected]a2b5ded2013-05-20 21:32:531841 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:271842 break;
1843 }
1844
1845 if (did_scroll) {
1846 client_->SetNeedsCommitOnImplThread();
1847 client_->SetNeedsRedrawOnImplThread();
1848 client_->RenewTreePriority();
[email protected]a2b5ded2013-05-20 21:32:531849
1850 // Scrolling of any layer will reset root overscroll accumulation.
1851 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]c1bb5af2013-03-13 19:06:271852 }
[email protected]a2b5ded2013-05-20 21:32:531853
1854 accumulated_root_overscroll_ += unused_root_delta;
1855 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
1856 if (did_overscroll && input_handler_client_) {
1857 input_handler_client_->DidOverscroll(accumulated_root_overscroll_,
1858 current_fling_velocity_);
1859 }
1860
[email protected]c1bb5af2013-03-13 19:06:271861 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:551862}
1863
[email protected]be782f52013-03-23 21:36:141864// This implements scrolling by page as described here:
1865// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
1866// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:491867bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
1868 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:141869 DCHECK(wheel_scrolling_);
1870
1871 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1872 layer_impl;
1873 layer_impl = layer_impl->parent()) {
1874 if (!layer_impl->scrollable())
1875 continue;
1876
1877 if (!layer_impl->vertical_scrollbar_layer())
1878 continue;
1879
1880 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
1881
1882 // These magical values match WebKit and are designed to scroll nearly the
1883 // entire visible content height but leave a bit of overlap.
1884 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:491885 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:141886 page = -page;
1887
1888 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
1889
1890 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
1891
1892 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:141893 client_->SetNeedsCommitOnImplThread();
1894 client_->SetNeedsRedrawOnImplThread();
1895 client_->RenewTreePriority();
1896 return true;
1897 }
1898
1899 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
1900 }
1901
1902 return false;
1903}
1904
[email protected]1960a712013-04-30 17:06:471905void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
1906 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
1907 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
1908 active_tree_->SetRootLayerScrollOffsetDelegate(
1909 root_layer_scroll_offset_delegate_);
1910}
1911
1912void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
1913 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
1914 client_->SetNeedsCommitOnImplThread();
1915}
1916
[email protected]c1bb5af2013-03-13 19:06:271917void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
1918 active_tree_->ClearCurrentlyScrollingLayer();
1919 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:531920 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:201921 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:141922}
1923
[email protected]c1bb5af2013-03-13 19:06:271924void LayerTreeHostImpl::ScrollEnd() {
1925 if (top_controls_manager_)
1926 top_controls_manager_->ScrollEnd();
1927 ClearCurrentlyScrollingLayer();
[email protected]fb7425a2013-04-22 16:28:551928 StartScrollbarAnimation(CurrentFrameTimeTicks());
[email protected]94f206c12012-08-25 00:09:141929}
1930
[email protected]5ff3c9782013-04-29 17:35:121931InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]7c45d8152013-04-23 18:27:211932 if (active_tree_->CurrentlyScrollingLayer())
1933 return ScrollStarted;
1934
1935 return ScrollIgnored;
1936}
1937
[email protected]6e921bd2013-04-29 21:10:201938void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
1939 current_fling_velocity_ = velocity;
1940}
1941
[email protected]c1bb5af2013-03-13 19:06:271942void LayerTreeHostImpl::PinchGestureBegin() {
1943 pinch_gesture_active_ = true;
1944 previous_pinch_anchor_ = gfx::Point();
1945 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:141946}
1947
[email protected]c1bb5af2013-03-13 19:06:271948void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
1949 gfx::Point anchor) {
1950 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:281951
[email protected]c1bb5af2013-03-13 19:06:271952 if (!RootScrollLayer())
1953 return;
[email protected]d3afa112012-12-08 06:24:281954
[email protected]c1bb5af2013-03-13 19:06:271955 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1956 // position over the course of the magnify.
1957 float page_scale_delta = active_tree_->page_scale_delta();
1958 gfx::PointF previous_scale_anchor =
1959 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1960 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
1961 page_scale_delta = active_tree_->page_scale_delta();
1962 gfx::PointF new_scale_anchor =
1963 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1964 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
1965
1966 previous_pinch_anchor_ = anchor;
1967
1968 move.Scale(1 / active_tree_->page_scale_factor());
1969
1970 RootScrollLayer()->ScrollBy(move);
1971
[email protected]c1bb5af2013-03-13 19:06:271972 client_->SetNeedsCommitOnImplThread();
1973 client_->SetNeedsRedrawOnImplThread();
1974 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:281975}
1976
[email protected]c1bb5af2013-03-13 19:06:271977void LayerTreeHostImpl::PinchGestureEnd() {
1978 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:271979 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141980}
1981
[email protected]c1bb5af2013-03-13 19:06:271982static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
1983 LayerImpl* layer_impl) {
1984 if (!layer_impl)
1985 return;
[email protected]94f206c12012-08-25 00:09:141986
[email protected]c1bb5af2013-03-13 19:06:271987 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:471988 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271989 if (!scroll_delta.IsZero()) {
1990 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:391991 scroll.layer_id = layer_impl->id();
1992 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:271993 scroll_info->scrolls.push_back(scroll);
1994 layer_impl->SetSentScrollDelta(scroll_delta);
1995 }
[email protected]94f206c12012-08-25 00:09:141996
[email protected]c1bb5af2013-03-13 19:06:271997 for (size_t i = 0; i < layer_impl->children().size(); ++i)
1998 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:141999}
2000
[email protected]c1bb5af2013-03-13 19:06:272001scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2002 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302003
[email protected]c1bb5af2013-03-13 19:06:272004 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392005 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2006 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302007
[email protected]c1bb5af2013-03-13 19:06:272008 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302009}
2010
[email protected]c1bb5af2013-03-13 19:06:272011void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]878705be2013-04-15 22:44:022012 SetViewportDamage(gfx::Rect(device_viewport_size_));
[email protected]829ad972013-01-28 23:36:102013}
2014
[email protected]c1bb5af2013-03-13 19:06:272015void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2016 if (!page_scale_animation_ || !RootScrollLayer())
2017 return;
2018
2019 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2020 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472021 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272022
2023 active_tree_->SetPageScaleDelta(
2024 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2025 active_tree_->page_scale_factor());
2026 gfx::Vector2dF next_scroll =
2027 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2028
2029 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2030 client_->SetNeedsRedrawOnImplThread();
2031
2032 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2033 page_scale_animation_.reset();
2034 client_->SetNeedsCommitOnImplThread();
2035 client_->RenewTreePriority();
2036 }
[email protected]829ad972013-01-28 23:36:102037}
2038
[email protected]ffb2720f2013-03-15 19:18:372039void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2040 if (!top_controls_manager_ || !RootScrollLayer())
2041 return;
2042 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2043 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172044 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2045 return;
[email protected]ffb2720f2013-03-15 19:18:372046 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2047 scroll, 1.f / active_tree_->total_page_scale_factor()));
2048}
2049
[email protected]c1bb5af2013-03-13 19:06:272050void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2051 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522052 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272053 animation_registrar_->active_animation_controllers().empty() ||
2054 !active_tree_->root_layer())
2055 return;
2056
2057 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2058
2059 last_animation_time_ = wall_clock_time;
2060 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2061
2062 AnimationRegistrar::AnimationControllerMap copy =
2063 animation_registrar_->active_animation_controllers();
2064 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2065 iter != copy.end();
2066 ++iter)
2067 (*iter).second->Animate(monotonic_seconds);
2068
2069 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082070}
2071
[email protected]3d9f7432013-04-06 00:35:182072void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522073 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272074 animation_registrar_->active_animation_controllers().empty() ||
2075 !active_tree_->root_layer())
2076 return;
2077
2078 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2079 scoped_ptr<AnimationEventsVector> events =
2080 make_scoped_ptr(new AnimationEventsVector);
2081 AnimationRegistrar::AnimationControllerMap copy =
2082 animation_registrar_->active_animation_controllers();
2083 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2084 iter != copy.end();
2085 ++iter)
[email protected]3d9f7432013-04-06 00:35:182086 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272087
2088 if (!events->empty()) {
2089 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2090 last_animation_time_);
2091 }
[email protected]131a0c22013-02-12 18:31:082092}
2093
[email protected]c1bb5af2013-03-13 19:06:272094base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2095 return base::TimeDelta::FromSeconds(1);
2096}
2097
[email protected]ff1211d2013-06-07 01:58:352098void LayerTreeHostImpl::SendDidLoseOutputSurfaceRecursive(LayerImpl* current) {
2099 DCHECK(current);
2100 current->DidLoseOutputSurface();
2101 if (current->mask_layer())
2102 SendDidLoseOutputSurfaceRecursive(current->mask_layer());
2103 if (current->replica_layer())
2104 SendDidLoseOutputSurfaceRecursive(current->replica_layer());
2105 for (size_t i = 0; i < current->children().size(); ++i)
2106 SendDidLoseOutputSurfaceRecursive(current->children()[i]);
2107}
2108
[email protected]c1bb5af2013-03-13 19:06:272109void LayerTreeHostImpl::ClearRenderSurfaces() {
2110 active_tree_->ClearRenderSurfaces();
2111 if (pending_tree_)
2112 pending_tree_->ClearRenderSurfaces();
2113}
2114
2115std::string LayerTreeHostImpl::LayerTreeAsText() const {
2116 std::string str;
2117 if (active_tree_->root_layer()) {
2118 str = active_tree_->root_layer()->LayerTreeAsText();
2119 str += "RenderSurfaces:\n";
2120 DumpRenderSurfaces(&str, 1, active_tree_->root_layer());
2121 }
2122 return str;
2123}
2124
2125std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2126 std::string str;
2127 if (active_tree_->root_layer()) {
2128 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2129 base::JSONWriter::WriteWithOptions(
2130 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2131 }
2132 return str;
2133}
2134
2135void LayerTreeHostImpl::DumpRenderSurfaces(std::string* str,
2136 int indent,
2137 const LayerImpl* layer) const {
2138 if (layer->render_surface())
2139 layer->render_surface()->DumpSurface(str, indent);
2140
2141 for (size_t i = 0; i < layer->children().size(); ++i)
2142 DumpRenderSurfaces(str, indent, layer->children()[i]);
2143}
2144
2145int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362146 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272147}
2148
[email protected]c1bb5af2013-03-13 19:06:272149void LayerTreeHostImpl::SendManagedMemoryStats(
2150 size_t memory_visible_bytes,
2151 size_t memory_visible_and_nearby_bytes,
2152 size_t memory_use_bytes) {
2153 if (!renderer_)
2154 return;
2155
2156 // Round the numbers being sent up to the next 8MB, to throttle the rate
2157 // at which we spam the GPU process.
2158 static const size_t rounding_step = 8 * 1024 * 1024;
2159 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2160 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2161 rounding_step);
2162 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2163 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2164 last_sent_memory_visible_and_nearby_bytes_ ==
2165 memory_visible_and_nearby_bytes &&
2166 last_sent_memory_use_bytes_ == memory_use_bytes) {
2167 return;
2168 }
2169 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2170 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2171 last_sent_memory_use_bytes_ = memory_use_bytes;
2172
2173 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2174 last_sent_memory_visible_and_nearby_bytes_,
2175 last_sent_memory_use_bytes_);
2176}
2177
2178void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2179 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2180}
2181
2182void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2183 base::TimeTicks time) {
2184 if (!layer)
2185 return;
2186
2187 ScrollbarAnimationController* scrollbar_controller =
2188 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352189 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202190 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422191 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2192 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272193 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202194 }
[email protected]c1bb5af2013-03-13 19:06:272195
2196 for (size_t i = 0; i < layer->children().size(); ++i)
2197 AnimateScrollbarsRecursive(layer->children()[i], time);
2198}
2199
[email protected]0fc818e2013-03-18 06:45:202200void LayerTreeHostImpl::StartScrollbarAnimation(base::TimeTicks time) {
2201 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
2202 StartScrollbarAnimationRecursive(RootLayer(), time);
2203}
2204
2205void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2206 base::TimeTicks time) {
2207 if (!layer)
2208 return;
2209
2210 ScrollbarAnimationController* scrollbar_controller =
2211 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352212 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2213 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202214 if (delay > base::TimeDelta())
2215 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352216 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202217 client_->SetNeedsRedrawOnImplThread();
2218 }
2219
2220 for (size_t i = 0; i < layer->children().size(); ++i)
2221 StartScrollbarAnimationRecursive(layer->children()[i], time);
2222}
2223
[email protected]c1bb5af2013-03-13 19:06:272224void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2225 if (!tile_manager_)
2226 return;
2227
2228 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2229 if (new_state.tree_priority == priority)
2230 return;
2231
2232 new_state.tree_priority = priority;
2233 tile_manager_->SetGlobalState(new_state);
2234}
2235
[email protected]8347d692013-05-17 23:22:382236void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552237 current_frame_timeticks_ = base::TimeTicks();
2238 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272239}
2240
[email protected]fb7425a2013-04-22 16:28:552241static void UpdateCurrentFrameTime(base::TimeTicks* ticks, base::Time* now) {
2242 if (ticks->is_null()) {
2243 DCHECK(now->is_null());
2244 *ticks = base::TimeTicks::Now();
2245 *now = base::Time::Now();
2246 }
2247}
2248
2249base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2250 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2251 return current_frame_timeticks_;
2252}
2253
2254base::Time LayerTreeHostImpl::CurrentFrameTime() {
2255 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272256 return current_frame_time_;
2257}
2258
2259scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
2260 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222261 if (this->pending_tree_)
2262 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272263 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212264 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272265 if (tile_manager_)
2266 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2267 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222268 if (pending_tree_)
2269 state->Set("pending_tree", pending_tree_->AsValue().release());
2270 return state.PassAs<base::Value>();
2271}
2272
2273scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222274 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2275 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472276 if (tile_manager_)
2277 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272278 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082279}
2280
[email protected]b9dcf43a2013-01-09 00:15:292281// static
[email protected]c1bb5af2013-03-13 19:06:272282LayerImpl* LayerTreeHostImpl::GetNonCompositedContentLayerRecursive(
2283 LayerImpl* layer) {
2284 if (!layer)
[email protected]b9dcf43a2013-01-09 00:15:292285 return NULL;
[email protected]c1bb5af2013-03-13 19:06:272286
2287 if (layer->DrawsContent())
2288 return layer;
2289
[email protected]50761e92013-03-29 20:51:282290 for (LayerImplList::const_iterator it = layer->children().begin();
[email protected]c1bb5af2013-03-13 19:06:272291 it != layer->children().end(); ++it) {
2292 LayerImpl* nccr = GetNonCompositedContentLayerRecursive(*it);
2293 if (nccr)
2294 return nccr;
2295 }
2296
2297 return NULL;
[email protected]b9dcf43a2013-01-09 00:15:292298}
2299
[email protected]c1bb5af2013-03-13 19:06:272300skia::RefPtr<SkPicture> LayerTreeHostImpl::CapturePicture() {
2301 LayerTreeImpl* tree =
2302 pending_tree_ ? pending_tree_.get() : active_tree_.get();
2303 LayerImpl* layer = GetNonCompositedContentLayerRecursive(tree->root_layer());
2304 return layer ? layer->GetPicture() : skia::RefPtr<SkPicture>();
[email protected]b9dcf43a2013-01-09 00:15:292305}
2306
[email protected]c1bb5af2013-03-13 19:06:272307void LayerTreeHostImpl::SetDebugState(const LayerTreeDebugState& debug_state) {
[email protected]846f455b2013-03-18 19:07:412308 if (debug_state_.continuous_painting != debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272309 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242310
[email protected]c1bb5af2013-03-13 19:06:272311 debug_state_ = debug_state;
[email protected]d0d12192013-02-08 19:02:022312}
2313
[email protected]d3143c732012-10-05 19:17:592314} // namespace cc