blob: b01aee6503763853b363a1b7bc42ac3a01032fd9 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
8
[email protected]ad5d1422012-10-19 13:40:299#include "base/basictypes.h"
[email protected]4a23c374c2012-12-08 08:38:5510#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1411#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3412#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4813#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0914#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4015#include "cc/animation/timing_function.h"
[email protected]681ccff2013-03-18 06:13:5216#include "cc/base/math_util.h"
17#include "cc/base/util.h"
[email protected]6e84de22013-03-18 06:54:2718#include "cc/debug/debug_rect_history.h"
19#include "cc/debug/frame_rate_counter.h"
20#include "cc/debug/overdraw_metrics.h"
21#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4322#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2223#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2124#include "cc/input/page_scale_animation.h"
25#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5226#include "cc/layers/append_quads_data.h"
27#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2828#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5229#include "cc/layers/layer_iterator.h"
[email protected]50761e92013-03-29 20:51:2830#include "cc/layers/render_surface_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/scrollbar_layer_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3032#include "cc/output/compositor_frame_metadata.h"
33#include "cc/output/delegating_renderer.h"
34#include "cc/output/gl_renderer.h"
35#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5636#include "cc/quads/render_pass_draw_quad.h"
37#include "cc/quads/shared_quad_state.h"
38#include "cc/quads/solid_color_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4039#include "cc/resources/memory_history.h"
40#include "cc/resources/picture_layer_tiling.h"
41#include "cc/resources/prioritized_resource_manager.h"
[email protected]be4655a2013-03-18 08:36:3142#include "cc/scheduler/delay_based_time_source.h"
43#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0444#include "cc/trees/damage_tracker.h"
45#include "cc/trees/layer_tree_host.h"
46#include "cc/trees/layer_tree_host_common.h"
47#include "cc/trees/layer_tree_impl.h"
48#include "cc/trees/quad_culler.h"
49#include "cc/trees/single_thread_proxy.h"
50#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0651#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1352#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1453
[email protected]94f206c12012-08-25 00:09:1454namespace {
55
[email protected]c1bb5af2013-03-13 19:06:2756void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
57 if (visible) {
58 TRACE_EVENT_ASYNC_BEGIN1("webkit",
59 "LayerTreeHostImpl::SetVisible",
60 id,
61 "LayerTreeHostImpl",
62 id);
63 return;
64 }
[email protected]94f206c12012-08-25 00:09:1465
[email protected]c1bb5af2013-03-13 19:06:2766 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1467}
68
[email protected]c1bb5af2013-03-13 19:06:2769} // namespace
[email protected]94f206c12012-08-25 00:09:1470
[email protected]9c88e562012-09-14 22:21:3071namespace cc {
[email protected]94f206c12012-08-25 00:09:1472
[email protected]96baf3e2012-10-22 23:09:5573class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:2774 public:
75 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
76 LayerTreeHostImpl* layer_tree_host_impl,
77 scoped_refptr<DelayBasedTimeSource> time_source) {
78 return make_scoped_ptr(
79 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
80 time_source));
81 }
82 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:2683 time_source_->SetClient(NULL);
84 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:2785 }
86
[email protected]6d0e69d2013-03-20 14:53:2687 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:2788 // In single threaded mode we attempt to simulate changing the current
89 // thread by maintaining a fake thread id. When we switch from one
90 // thread to another, we construct DebugScopedSetXXXThread objects that
91 // update the thread id. This lets DCHECKS that ensure we're on the
92 // right thread to work correctly in single threaded mode. The problem
93 // here is that the timer tasks are run via the message loop, and when
94 // they run, we've had no chance to construct a DebugScopedSetXXXThread
95 // object. The result is that we report that we're running on the main
96 // thread. In multi-threaded mode, this timer is run on the compositor
97 // thread, so to keep this consistent in single-threaded mode, we'll
98 // construct a DebugScopedSetImplThread object. There is no need to do
99 // this in multi-threaded mode since the real thread id's will be
100 // correct. In fact, setting fake thread id's interferes with the real
101 // thread id's and causes breakage.
102 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
103 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
104 set_impl_thread.reset(
105 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14106 }
107
[email protected]94bf75c2013-06-12 13:20:04108 // TODO(enne): This should probably happen post-animate.
109 if (layer_tree_host_impl_->pending_tree()) {
110 layer_tree_host_impl_->ActivatePendingTreeIfNeeded();
111
112 if (layer_tree_host_impl_->pending_tree()) {
113 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
114 layer_tree_host_impl_->ManageTiles();
115 }
116 }
117
[email protected]fb7425a2013-04-22 16:28:55118 layer_tree_host_impl_->Animate(
119 layer_tree_host_impl_->CurrentFrameTimeTicks(),
120 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18121 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
122 bool start_ready_animations = true;
123 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38124 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27125 }
[email protected]373974232013-01-10 22:20:50126
[email protected]c1bb5af2013-03-13 19:06:27127 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26128 if (active != time_source_->Active())
129 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27130 }
[email protected]94f206c12012-08-25 00:09:14131
[email protected]c1bb5af2013-03-13 19:06:27132 private:
133 LayerTreeHostImplTimeSourceAdapter(
134 LayerTreeHostImpl* layer_tree_host_impl,
135 scoped_refptr<DelayBasedTimeSource> time_source)
136 : layer_tree_host_impl_(layer_tree_host_impl),
137 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26138 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27139 }
[email protected]94f206c12012-08-25 00:09:14140
[email protected]c1bb5af2013-03-13 19:06:27141 LayerTreeHostImpl* layer_tree_host_impl_;
142 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14143
[email protected]c1bb5af2013-03-13 19:06:27144 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14145};
146
[email protected]96baf3e2012-10-22 23:09:55147LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20148 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27149
150LayerTreeHostImpl::FrameData::~FrameData() {}
151
152scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
153 const LayerTreeSettings& settings,
154 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43155 Proxy* proxy,
156 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
157 return make_scoped_ptr(
158 new LayerTreeHostImpl(settings,
159 client,
160 proxy,
161 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10162}
163
[email protected]372bad5f2013-03-21 16:38:43164LayerTreeHostImpl::LayerTreeHostImpl(
165 const LayerTreeSettings& settings,
166 LayerTreeHostImplClient* client,
167 Proxy* proxy,
168 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27169 : client_(client),
170 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37171 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27172 did_lock_scrolling_layer_(false),
173 should_bubble_scrolls_(false),
174 wheel_scrolling_(false),
[email protected]a23451e2013-06-07 20:58:26175 manage_tiles_needed_(false),
[email protected]1960a712013-04-30 17:06:47176 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27177 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27178 visible_(true),
179 managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34180 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27181 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
182 0,
183 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING),
184 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36185 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29186 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27187 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02188 debug_rect_history_(DebugRectHistory::Create()),
[email protected]d7626ffd2013-03-29 00:17:42189 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27190 last_sent_memory_visible_bytes_(0),
191 last_sent_memory_visible_and_nearby_bytes_(0),
192 last_sent_memory_use_bytes_(0),
[email protected]f224cc92013-06-06 23:23:32193 device_scale_factor_(1.f),
194 overdraw_bottom_height_(0.f),
[email protected]372bad5f2013-03-21 16:38:43195 animation_registrar_(AnimationRegistrar::Create()),
196 rendering_stats_instrumentation_(rendering_stats_instrumentation) {
[email protected]c1bb5af2013-03-13 19:06:27197 DCHECK(proxy_->IsImplThread());
198 DidVisibilityChange(this, visible_);
199
[email protected]8e0176d2013-03-21 03:14:52200 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27201
[email protected]8e0176d2013-03-21 03:14:52202 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27203 top_controls_manager_ =
204 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52205 settings.top_controls_height,
206 settings.top_controls_show_threshold,
207 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27208 }
209
[email protected]8e0176d2013-03-21 03:14:52210 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27211
212 // LTHI always has an active tree.
213 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51214 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
215 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10216}
217
[email protected]c1bb5af2013-03-13 19:06:27218LayerTreeHostImpl::~LayerTreeHostImpl() {
219 DCHECK(proxy_->IsImplThread());
220 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51221 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
222 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27223
[email protected]200a9c062013-05-20 04:34:37224 if (input_handler_client_) {
225 input_handler_client_->WillShutdown();
226 input_handler_client_ = NULL;
227 }
228
[email protected]c1bb5af2013-03-13 19:06:27229 if (active_tree_->root_layer()) {
230 ClearRenderSurfaces();
231 // The layer trees must be destroyed before the layer tree host. We've
232 // made a contract with our animation controllers that the registrar
233 // will outlive them, and we must make good.
234 recycle_tree_.reset();
235 pending_tree_.reset();
236 active_tree_.reset();
237 }
[email protected]94f206c12012-08-25 00:09:14238}
239
[email protected]c1bb5af2013-03-13 19:06:27240void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29241
[email protected]c1bb5af2013-03-13 19:06:27242void LayerTreeHostImpl::CommitComplete() {
243 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08244
[email protected]c1bb5af2013-03-13 19:06:27245 // Impl-side painting needs an update immediately post-commit to have the
246 // opportunity to create tilings. Other paths can call UpdateDrawProperties
247 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52248 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27249 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04250 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26251 // Start working on newly created tiles immediately if needed.
252 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27253 } else {
254 active_tree_->set_needs_update_draw_properties();
255 }
[email protected]3ba4cae2013-01-16 03:58:38256
[email protected]c1bb5af2013-03-13 19:06:27257 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14258}
259
[email protected]c1bb5af2013-03-13 19:06:27260bool LayerTreeHostImpl::CanDraw() {
261 // Note: If you are changing this function or any other function that might
262 // affect the result of CanDraw, make sure to call
263 // client_->OnCanDrawStateChanged in the proper places and update the
264 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14265
[email protected]c1bb5af2013-03-13 19:06:27266 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42267 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
268 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22269 return false;
[email protected]c1bb5af2013-03-13 19:06:27270 }
271 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42272 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
273 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27274 return false;
275 }
276 if (active_tree_->ViewportSizeInvalid()) {
277 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42278 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
279 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27280 return false;
281 }
282 if (!renderer_) {
[email protected]c76faea2013-03-26 07:42:42283 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
284 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27285 return false;
286 }
287 if (active_tree_->ContentsTexturesPurged()) {
288 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42289 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
290 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27291 return false;
292 }
293 return true;
[email protected]2f1acc262012-11-16 21:42:22294}
295
[email protected]c1bb5af2013-03-13 19:06:27296void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
297 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37298 if (input_handler_client_)
299 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27300 AnimatePageScale(monotonic_time);
301 AnimateLayers(monotonic_time, wall_clock_time);
302 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37303 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14304}
305
[email protected]c1bb5af2013-03-13 19:06:27306void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26307 if (!tile_manager_)
308 return;
309 if (!manage_tiles_needed_)
310 return;
311 manage_tiles_needed_ = false;
[email protected]c1bb5af2013-03-13 19:06:27312 tile_manager_->ManageTiles();
313
314 size_t memory_required_bytes;
315 size_t memory_nice_to_have_bytes;
316 size_t memory_used_bytes;
317 tile_manager_->GetMemoryStats(&memory_required_bytes,
318 &memory_nice_to_have_bytes,
319 &memory_used_bytes);
320 SendManagedMemoryStats(memory_required_bytes,
321 memory_nice_to_have_bytes,
322 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15323}
324
[email protected]c1bb5af2013-03-13 19:06:27325void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
326 bool anchor_point,
327 float page_scale,
328 base::TimeTicks start_time,
329 base::TimeDelta duration) {
330 if (!RootScrollLayer())
331 return;
332
333 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47334 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27335 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51336 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27337
338 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40339
340 // Easing constants experimentally determined.
341 scoped_ptr<TimingFunction> timing_function =
342 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
343
[email protected]c1bb5af2013-03-13 19:06:27344 page_scale_animation_ =
345 PageScaleAnimation::Create(scroll_total,
346 active_tree_->total_page_scale_factor(),
347 viewport_size,
348 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40349 start_time_seconds,
350 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27351
352 if (anchor_point) {
353 gfx::Vector2dF anchor(target_offset);
354 page_scale_animation_->ZoomWithAnchor(anchor,
355 page_scale,
356 duration.InSecondsF());
357 } else {
358 gfx::Vector2dF scaled_target_offset = target_offset;
359 page_scale_animation_->ZoomTo(scaled_target_offset,
360 page_scale,
361 duration.InSecondsF());
362 }
363
364 client_->SetNeedsRedrawOnImplThread();
365 client_->SetNeedsCommitOnImplThread();
366 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15367}
368
[email protected]c1bb5af2013-03-13 19:06:27369void LayerTreeHostImpl::ScheduleAnimation() {
370 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15371}
372
[email protected]c1bb5af2013-03-13 19:06:27373bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
374 if (!EnsureRenderSurfaceLayerList())
375 return false;
[email protected]f57bbc02012-11-21 07:02:15376
[email protected]c1bb5af2013-03-13 19:06:27377 gfx::PointF device_viewport_point =
378 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15379
[email protected]c1bb5af2013-03-13 19:06:27380 // First find out which layer was hit from the saved list of visible layers
381 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39382 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27383 device_viewport_point,
384 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15385
[email protected]c1bb5af2013-03-13 19:06:27386 // Walk up the hierarchy and look for a layer with a touch event handler
387 // region that the given point hits.
388 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39389 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27390 layer_impl))
391 return true;
392 }
[email protected]f57bbc02012-11-21 07:02:15393
[email protected]c1bb5af2013-03-13 19:06:27394 return false;
395}
396
[email protected]b76329d2013-06-11 04:15:08397void LayerTreeHostImpl::SetLatencyInfoForInputEvent(
398 const ui::LatencyInfo& latency_info) {
399 active_tree()->SetLatencyInfo(latency_info);
400}
401
[email protected]c1bb5af2013-03-13 19:06:27402void LayerTreeHostImpl::TrackDamageForAllSurfaces(
403 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28404 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27405 // For now, we use damage tracking to compute a global scissor. To do this, we
406 // must compute all damage tracking before drawing anything, so that we know
407 // the root damage rect. The root damage rect is then used to scissor each
408 // surface.
409
410 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06411 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27412 --surface_index) {
413 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
414 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
415 DCHECK(render_surface);
416 render_surface->damage_tracker()->UpdateDamageTrackingState(
417 render_surface->layer_list(),
418 render_surface_layer->id(),
419 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
420 render_surface->content_rect(),
421 render_surface_layer->mask_layer(),
422 render_surface_layer->filters(),
423 render_surface_layer->filter().get());
424 }
425}
426
427void LayerTreeHostImpl::FrameData::AppendRenderPass(
428 scoped_ptr<RenderPass> render_pass) {
429 render_passes_by_id[render_pass->id] = render_pass.get();
430 render_passes.push_back(render_pass.Pass());
431}
432
[email protected]ffbb2212013-06-02 23:47:59433static DrawMode GetDrawMode(OutputSurface* output_surface) {
434 if (output_surface->ForcedDrawToSoftwareDevice()) {
435 return DRAW_MODE_RESOURCELESS_SOFTWARE;
436 } else if (output_surface->context3d()) {
437 return DRAW_MODE_HARDWARE;
438 } else {
439 DCHECK(output_surface->software_device());
440 return DRAW_MODE_SOFTWARE;
441 }
442}
443
[email protected]c1bb5af2013-03-13 19:06:27444static void AppendQuadsForLayer(RenderPass* target_render_pass,
445 LayerImpl* layer,
446 const OcclusionTrackerImpl& occlusion_tracker,
447 AppendQuadsData* append_quads_data) {
448 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49449 QuadCuller quad_culler(&target_render_pass->quad_list,
450 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27451 layer,
452 occlusion_tracker,
453 layer->ShowDebugBorders(),
454 for_surface);
455 layer->AppendQuads(&quad_culler, append_quads_data);
456}
457
458static void AppendQuadsForRenderSurfaceLayer(
459 RenderPass* target_render_pass,
460 LayerImpl* layer,
461 const RenderPass* contributing_render_pass,
462 const OcclusionTrackerImpl& occlusion_tracker,
463 AppendQuadsData* append_quads_data) {
464 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49465 QuadCuller quad_culler(&target_render_pass->quad_list,
466 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27467 layer,
468 occlusion_tracker,
469 layer->ShowDebugBorders(),
470 for_surface);
471
472 bool is_replica = false;
473 layer->render_surface()->AppendQuads(&quad_culler,
474 append_quads_data,
475 is_replica,
476 contributing_render_pass->id);
477
478 // Add replica after the surface so that it appears below the surface.
479 if (layer->has_replica()) {
480 is_replica = true;
481 layer->render_surface()->AppendQuads(&quad_culler,
482 append_quads_data,
483 is_replica,
484 contributing_render_pass->id);
485 }
486}
487
488static void AppendQuadsToFillScreen(
489 RenderPass* target_render_pass,
490 LayerImpl* root_layer,
491 SkColor screen_background_color,
492 const OcclusionTrackerImpl& occlusion_tracker) {
493 if (!root_layer || !SkColorGetA(screen_background_color))
494 return;
495
496 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
497 if (fill_region.IsEmpty())
498 return;
499
500 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49501 QuadCuller quad_culler(&target_render_pass->quad_list,
502 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27503 root_layer,
504 occlusion_tracker,
505 root_layer->ShowDebugBorders(),
506 for_surface);
507
508 // Manually create the quad state for the gutter quads, as the root layer
509 // doesn't have any bounds and so can't generate this itself.
510 // TODO(danakj): Make the gutter quads generated by the solid color layer
511 // (make it smarter about generating quads to fill unoccluded areas).
512
513 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
514 float opacity = 1.f;
515 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49516 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27517 shared_quad_state->SetAll(root_layer->draw_transform(),
518 root_target_rect.size(),
519 root_target_rect,
520 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01521 false,
[email protected]f57bbc02012-11-21 07:02:15522 opacity);
523
[email protected]c1bb5af2013-03-13 19:06:27524 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17525
[email protected]c1bb5af2013-03-13 19:06:27526 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
527 bool did_invert = root_layer->screen_space_transform().GetInverse(
528 &transform_to_layer_space);
529 DCHECK(did_invert);
530 for (Region::Iterator fill_rects(fill_region);
531 fill_rects.has_rect();
532 fill_rects.next()) {
533 // The root layer transform is composed of translations and scales only,
534 // no perspective, so mapping is sufficient (as opposed to projecting).
535 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21536 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27537 // Skip the quad culler and just append the quads directly to avoid
538 // occlusion checks.
539 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]10a30b12013-05-02 16:42:11540 quad->SetNew(shared_quad_state, layer_rect, screen_background_color, false);
[email protected]c7e95b42013-03-18 01:13:49541 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27542 }
[email protected]467b3612012-08-28 07:41:16543}
544
[email protected]c1bb5af2013-03-13 19:06:27545bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
546 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14547
[email protected]c1bb5af2013-03-13 19:06:27548 if (!CanDraw() || !active_tree_->root_layer())
549 return false;
[email protected]2d692992012-12-19 01:19:32550
[email protected]c1bb5af2013-03-13 19:06:27551 TrackDamageForAllSurfaces(active_tree_->root_layer(),
552 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14553
[email protected]e0341352013-04-06 05:01:20554 // If the root render surface has no visible damage, then don't generate a
555 // frame at all.
556 RenderSurfaceImpl* root_surface =
557 active_tree_->root_layer()->render_surface();
558 bool root_surface_has_no_visible_damage =
559 !root_surface->damage_tracker()->current_damage_rect().Intersects(
560 root_surface->content_rect());
561 bool root_surface_has_contributing_layers =
562 !root_surface->layer_list().empty();
563 if (root_surface_has_contributing_layers &&
564 root_surface_has_no_visible_damage) {
565 TRACE_EVENT0("cc",
566 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
567 frame->has_no_damage = true;
568 return true;
569 }
570
[email protected]c1bb5af2013-03-13 19:06:27571 TRACE_EVENT1("cc",
572 "LayerTreeHostImpl::CalculateRenderPasses",
573 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06574 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14575
[email protected]c1bb5af2013-03-13 19:06:27576 // Create the render passes in dependency order.
577 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06578 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27579 --surface_index) {
580 LayerImpl* render_surface_layer =
581 (*frame->render_surface_layer_list)[surface_index];
582 render_surface_layer->render_surface()->AppendRenderPasses(frame);
583 }
[email protected]94f206c12012-08-25 00:09:14584
[email protected]c1bb5af2013-03-13 19:06:27585 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52586 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27587 base::debug::TraceLog::GetInstance() &&
588 base::debug::TraceLog::GetInstance()->IsEnabled();
589 OcclusionTrackerImpl occlusion_tracker(
590 active_tree_->root_layer()->render_surface()->content_rect(),
591 record_metrics_for_frame);
592 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52593 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14594
[email protected]846f455b2013-03-18 19:07:41595 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27596 occlusion_tracker.set_occluding_screen_space_rects_container(
597 &frame->occluding_screen_space_rects);
598 }
[email protected]846f455b2013-03-18 19:07:41599 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27600 occlusion_tracker.set_non_occluding_screen_space_rects_container(
601 &frame->non_occluding_screen_space_rects);
602 }
[email protected]94f206c12012-08-25 00:09:14603
[email protected]c1bb5af2013-03-13 19:06:27604 // Add quads to the Render passes in FrontToBack order to allow for testing
605 // occlusion and performing culling during the tree walk.
606 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28607 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27608 RenderSurfaceImpl,
609 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14610
[email protected]c1bb5af2013-03-13 19:06:27611 // Typically when we are missing a texture and use a checkerboard quad, we
612 // still draw the frame. However when the layer being checkerboarded is moving
613 // due to an impl-animation, we drop the frame to avoid flashing due to the
614 // texture suddenly appearing in the future.
615 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25616 // When we have a copy request for a layer, we need to draw no matter
617 // what, as the layer may disappear after this frame.
618 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14619
[email protected]372bad5f2013-03-21 16:38:43620 int layers_drawn = 0;
621
[email protected]ffbb2212013-06-02 23:47:59622 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
623
[email protected]c1bb5af2013-03-13 19:06:27624 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09625 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27626 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09627 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27628 it != end;
629 ++it) {
630 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09631 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27632 RenderPass* target_render_pass =
633 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14634
[email protected]0e5f7142013-05-24 06:45:36635 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23636 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14637
[email protected]c1bb5af2013-03-13 19:06:27638 AppendQuadsData append_quads_data(target_render_pass->id);
[email protected]89228202012-08-29 03:20:30639
[email protected]18a70192013-04-26 16:18:25640 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36641 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25642 have_copy_request = true;
[email protected]0e5f7142013-05-24 06:45:36643 it->TakeCopyRequests(&target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25644 }
645 } else if (it.represents_contributing_render_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27646 RenderPass::Id contributing_render_pass_id =
647 it->render_surface()->RenderPassId();
648 RenderPass* contributing_render_pass =
649 frame->render_passes_by_id[contributing_render_pass_id];
650 AppendQuadsForRenderSurfaceLayer(target_render_pass,
651 *it,
652 contributing_render_pass,
653 occlusion_tracker,
654 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09655 } else if (it.represents_itself() &&
656 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27657 bool has_occlusion_from_outside_target_surface;
658 bool impl_draw_transform_is_unknown = false;
659 if (occlusion_tracker.Occluded(
660 it->render_target(),
661 it->visible_content_rect(),
662 it->draw_transform(),
663 impl_draw_transform_is_unknown,
664 it->is_clipped(),
665 it->clip_rect(),
666 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09667 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27668 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59669 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27670 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59671
[email protected]c1bb5af2013-03-13 19:06:27672 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57673
[email protected]c1bb5af2013-03-13 19:06:27674 if (it->HasContributingDelegatedRenderPasses()) {
675 RenderPass::Id contributing_render_pass_id =
676 it->FirstContributingRenderPassId();
677 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
678 frame->render_passes_by_id.end()) {
679 RenderPass* render_pass =
680 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14681
[email protected]c1bb5af2013-03-13 19:06:27682 AppendQuadsData append_quads_data(render_pass->id);
683 AppendQuadsForLayer(render_pass,
684 *it,
685 occlusion_tracker,
686 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57687
[email protected]c1bb5af2013-03-13 19:06:27688 contributing_render_pass_id =
689 it->NextContributingRenderPassId(contributing_render_pass_id);
690 }
[email protected]94f206c12012-08-25 00:09:14691 }
692
[email protected]c1bb5af2013-03-13 19:06:27693 AppendQuadsForLayer(target_render_pass,
694 *it,
695 occlusion_tracker,
696 &append_quads_data);
697 }
[email protected]89228202012-08-29 03:20:30698
[email protected]372bad5f2013-03-21 16:38:43699 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14700 }
701
[email protected]e1e768f2013-03-26 08:48:09702 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27703 target_render_pass->has_occlusion_from_outside_target_surface = true;
704
[email protected]e1e768f2013-03-26 08:48:09705 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43706 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09707 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27708 bool layer_has_animating_transform =
709 it->screen_space_transform_is_animating() ||
710 it->draw_transform_is_animating();
711 if (layer_has_animating_transform)
712 draw_frame = false;
713 }
714
[email protected]e1e768f2013-03-26 08:48:09715 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27716 frame->contains_incomplete_tile = true;
717
718 occlusion_tracker.LeaveLayer(it);
719 }
720
[email protected]18a70192013-04-26 16:18:25721 if (have_copy_request)
722 draw_frame = true;
723
[email protected]372bad5f2013-03-21 16:38:43724 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
725
[email protected]1d993172012-10-18 18:15:04726#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27727 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
728 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
729 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
730 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
731 != frame->render_passes_by_id.end());
732 }
[email protected]94f206c12012-08-25 00:09:14733#endif
[email protected]c1bb5af2013-03-13 19:06:27734 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14735
[email protected]c1bb5af2013-03-13 19:06:27736 if (!active_tree_->has_transparent_background()) {
737 frame->render_passes.back()->has_transparent_background = false;
738 AppendQuadsToFillScreen(frame->render_passes.back(),
739 active_tree_->root_layer(),
740 active_tree_->background_color(),
741 occlusion_tracker);
742 }
[email protected]94f206c12012-08-25 00:09:14743
[email protected]c1bb5af2013-03-13 19:06:27744 if (draw_frame)
745 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25746 else
747 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14748
[email protected]c1bb5af2013-03-13 19:06:27749 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37750 if (!output_surface_->ForcedDrawToSoftwareDevice())
751 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]fbe89f72013-05-21 07:24:24752 if (renderer_) {
753 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
754 frame);
755 }
[email protected]94f206c12012-08-25 00:09:14756
[email protected]e0341352013-04-06 05:01:20757 // If we're making a frame to draw, it better have at least one render pass.
758 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27759 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14760}
761
[email protected]200a9c062013-05-20 04:34:37762void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
763 if (input_handler_client_)
764 input_handler_client_->MainThreadHasStoppedFlinging();
765}
766
[email protected]3d9f7432013-04-06 00:35:18767void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
768 bool should_background_tick) {
769 bool enabled = should_background_tick &&
770 !animation_registrar_->active_animation_controllers().empty();
771
[email protected]c1bb5af2013-03-13 19:06:27772 // Lazily create the time_source adapter so that we can vary the interval for
773 // testing.
774 if (!time_source_client_adapter_) {
775 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
776 this,
[email protected]6d0e69d2013-03-20 14:53:26777 DelayBasedTimeSource::Create(LowFrequencyAnimationInterval(),
[email protected]c1bb5af2013-03-13 19:06:27778 proxy_->CurrentThread()));
779 }
[email protected]94f206c12012-08-25 00:09:14780
[email protected]c1bb5af2013-03-13 19:06:27781 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14782}
783
[email protected]b9d4a362013-04-23 05:36:27784void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02785 viewport_damage_rect_.Union(damage_rect);
786}
787
[email protected]c1bb5af2013-03-13 19:06:27788static inline RenderPass* FindRenderPassById(
789 RenderPass::Id render_pass_id,
790 const LayerTreeHostImpl::FrameData& frame) {
791 RenderPassIdHashMap::const_iterator it =
792 frame.render_passes_by_id.find(render_pass_id);
793 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14794}
795
[email protected]c1bb5af2013-03-13 19:06:27796static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
797 LayerTreeHostImpl::FrameData* frame) {
798 RenderPass* remove_render_pass =
799 FindRenderPassById(remove_render_pass_id, *frame);
800 // The pass was already removed by another quad - probably the original, and
801 // we are the replica.
802 if (!remove_render_pass)
803 return;
804 RenderPassList& render_passes = frame->render_passes;
805 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
806 render_passes.end(),
807 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14808
[email protected]c1bb5af2013-03-13 19:06:27809 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14810
[email protected]c1bb5af2013-03-13 19:06:27811 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
812 frame->render_passes.erase(to_remove);
813 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14814
[email protected]c1bb5af2013-03-13 19:06:27815 // Now follow up for all RenderPass quads and remove their RenderPasses
816 // recursively.
817 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29818 QuadList::ConstBackToFrontIterator quad_list_iterator =
819 quad_list.BackToFrontBegin();
820 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27821 ++quad_list_iterator) {
822 DrawQuad* current_quad = (*quad_list_iterator);
823 if (current_quad->material != DrawQuad::RENDER_PASS)
824 continue;
[email protected]94f206c12012-08-25 00:09:14825
[email protected]c1bb5af2013-03-13 19:06:27826 RenderPass::Id next_remove_render_pass_id =
827 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
828 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
829 }
[email protected]94f206c12012-08-25 00:09:14830}
831
[email protected]c1bb5af2013-03-13 19:06:27832bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
833 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
834 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24835 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27836 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
837 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06838 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27839 if (quad_has_damage) {
840 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
841 return false;
842 } else if (!quad_has_cached_resource) {
843 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
844 return false;
845 }
846 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
847 return true;
[email protected]94f206c12012-08-25 00:09:14848}
849
[email protected]c1bb5af2013-03-13 19:06:27850bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
851 const RenderPassDrawQuad& quad, const FrameData& frame) const {
852 const RenderPass* render_pass =
853 FindRenderPassById(quad.render_pass_id, frame);
854 if (!render_pass)
855 return false;
[email protected]94f206c12012-08-25 00:09:14856
[email protected]c1bb5af2013-03-13 19:06:27857 // If any quad or RenderPass draws into this RenderPass, then keep it.
858 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29859 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
860 quad_list.BackToFrontBegin();
861 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27862 ++quad_list_iterator) {
863 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14864
[email protected]c1bb5af2013-03-13 19:06:27865 if (current_quad->material != DrawQuad::RENDER_PASS)
866 return false;
[email protected]94f206c12012-08-25 00:09:14867
[email protected]c1bb5af2013-03-13 19:06:27868 const RenderPass* contributing_pass = FindRenderPassById(
869 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
870 if (contributing_pass)
871 return false;
872 }
873 return true;
[email protected]94f206c12012-08-25 00:09:14874}
875
876// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27877template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
878 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
879 CullRenderPassesWithCachedTextures culler, FrameData* frame);
880template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
881 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
882 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14883
884// static
[email protected]c1bb5af2013-03-13 19:06:27885template <typename RenderPassCuller>
886void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
887 FrameData* frame) {
888 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
889 it != culler.RenderPassListEnd(frame->render_passes);
890 it = culler.RenderPassListNext(it)) {
891 const RenderPass* current_pass = frame->render_passes[it];
892 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29893 QuadList::ConstBackToFrontIterator quad_list_iterator =
894 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14895
[email protected]ed511b8d2013-03-25 03:29:29896 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27897 ++quad_list_iterator) {
898 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14899
[email protected]c1bb5af2013-03-13 19:06:27900 if (current_quad->material != DrawQuad::RENDER_PASS)
901 continue;
[email protected]94f206c12012-08-25 00:09:14902
[email protected]c1bb5af2013-03-13 19:06:27903 const RenderPassDrawQuad* render_pass_quad =
904 RenderPassDrawQuad::MaterialCast(current_quad);
905 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
906 continue;
[email protected]94f206c12012-08-25 00:09:14907
[email protected]c1bb5af2013-03-13 19:06:27908 // We are changing the vector in the middle of iteration. Because we
909 // delete render passes that draw into the current pass, we are
910 // guaranteed that any data from the iterator to the end will not
911 // change. So, capture the iterator position from the end of the
912 // list, and restore it after the change.
913 size_t position_from_end = frame->render_passes.size() - it;
914 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
915 it = frame->render_passes.size() - position_from_end;
916 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14917 }
[email protected]c1bb5af2013-03-13 19:06:27918 }
[email protected]94f206c12012-08-25 00:09:14919}
920
[email protected]e0341352013-04-06 05:01:20921bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
922 gfx::Rect device_viewport_damage_rect) {
[email protected]c1bb5af2013-03-13 19:06:27923 TRACE_EVENT0("cc", "LayerTreeHostImpl::PrepareToDraw");
[email protected]94f206c12012-08-25 00:09:14924
[email protected]7d19dc342013-05-02 22:02:04925 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:27926
[email protected]c1bb5af2013-03-13 19:06:27927 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
928 frame->render_passes.clear();
929 frame->render_passes_by_id.clear();
930 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:20931 frame->contains_incomplete_tile = false;
932 frame->has_no_damage = false;
933
934 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:02935 device_viewport_damage_rect.Union(viewport_damage_rect_);
936 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:20937
938 active_tree_->root_layer()->render_surface()->damage_tracker()->
939 AddDamageNextUpdate(device_viewport_damage_rect);
940 }
[email protected]94f206c12012-08-25 00:09:14941
[email protected]c1bb5af2013-03-13 19:06:27942 if (!CalculateRenderPasses(frame))
943 return false;
[email protected]94f206c12012-08-25 00:09:14944
[email protected]c1bb5af2013-03-13 19:06:27945 // If we return true, then we expect DrawLayers() to be called before this
946 // function is called again.
947 return true;
[email protected]94f206c12012-08-25 00:09:14948}
949
[email protected]c1bb5af2013-03-13 19:06:27950void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
951 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:12952
[email protected]c1bb5af2013-03-13 19:06:27953 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:35954 visible_ ? policy.bytes_limit_when_visible
955 : policy.bytes_limit_when_not_visible,
956 ManagedMemoryPolicy::PriorityCutoffToValue(
957 visible_ ? policy.priority_cutoff_when_visible
958 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:27959 if (evicted_resources) {
960 active_tree_->SetContentsTexturesPurged();
961 if (pending_tree_)
962 pending_tree_->SetContentsTexturesPurged();
963 client_->SetNeedsCommitOnImplThread();
964 client_->OnCanDrawStateChanged(CanDraw());
965 client_->RenewTreePriority();
966 }
967 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:43968
[email protected]8be1a9bf2013-05-01 03:45:19969 UpdateTileManagerMemoryPolicy(policy);
970}
971
972void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
973 const ManagedMemoryPolicy& policy) {
974 if (!tile_manager_)
975 return;
976
977 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
978 new_state.memory_limit_in_bytes = visible_ ?
979 policy.bytes_limit_when_visible :
980 policy.bytes_limit_when_not_visible;
981 // TODO(reveman): We should avoid keeping around unused resources if
982 // possible. crbug.com/224475
983 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
984 (static_cast<int64>(new_state.memory_limit_in_bytes) *
985 settings_.max_unused_resource_memory_percentage) / 100);
986 new_state.memory_limit_policy =
987 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
988 visible_ ?
989 policy.priority_cutoff_when_visible :
990 policy.priority_cutoff_when_not_visible);
991 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:26992 manage_tiles_needed_ = true;
[email protected]94f206c12012-08-25 00:09:14993}
994
[email protected]c1bb5af2013-03-13 19:06:27995bool LayerTreeHostImpl::HasImplThread() const {
996 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:44997}
998
[email protected]86126792013-03-16 20:07:54999void LayerTreeHostImpl::DidInitializeVisibleTile() {
1000 // TODO(reveman): Determine tiles that changed and only damage
1001 // what's necessary.
1002 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:271003 if (client_)
[email protected]86126792013-03-16 20:07:541004 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471005}
1006
[email protected]fcb846d2013-05-22 01:42:361007bool LayerTreeHostImpl::
1008 ShouldForceTileUploadsRequiredForActivationToComplete() const {
1009 // During shutdown of TileManager, it will attempt to flush its job queue,
1010 // which can call this function while this is NULL.
1011 if (!tile_manager_)
1012 return false;
1013
1014 TreePriority tree_priority = tile_manager_->GlobalState().tree_priority;
1015 return tree_priority != SMOOTHNESS_TAKES_PRIORITY &&
1016 animation_registrar_->active_animation_controllers().empty();
1017}
1018
[email protected]94bf75c2013-06-12 13:20:041019void LayerTreeHostImpl::NotifyReadyToActivate() {
1020 if (pending_tree_)
1021 ActivatePendingTree();
1022}
1023
[email protected]c1bb5af2013-03-13 19:06:271024bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:521025 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:521026}
1027
[email protected]c1bb5af2013-03-13 19:06:271028void LayerTreeHostImpl::SetManagedMemoryPolicy(
1029 const ManagedMemoryPolicy& policy) {
1030 if (managed_memory_policy_ == policy)
1031 return;
[email protected]61de5812012-11-08 07:03:441032
[email protected]d7626ffd2013-03-29 00:17:421033 // If there is already enough memory to draw everything imaginable and the
1034 // new memory limit does not change this, then do not re-commit. Don't bother
1035 // skipping commits if this is not visible (commits don't happen when not
1036 // visible, there will almost always be a commit when this becomes visible).
1037 bool needs_commit = true;
1038 if (visible() &&
1039 policy.bytes_limit_when_visible >=
1040 max_memory_needed_bytes_ &&
1041 managed_memory_policy_.bytes_limit_when_visible >=
1042 max_memory_needed_bytes_ &&
1043 policy.priority_cutoff_when_visible ==
1044 managed_memory_policy_.priority_cutoff_when_visible) {
1045 needs_commit = false;
1046 }
1047
[email protected]c1bb5af2013-03-13 19:06:271048 managed_memory_policy_ = policy;
1049 if (!proxy_->HasImplThread()) {
[email protected]d7626ffd2013-03-29 00:17:421050 // In single-thread mode, this can be called on the main thread by
1051 // GLRenderer::OnMemoryAllocationChanged.
[email protected]c1bb5af2013-03-13 19:06:271052 DebugScopedSetImplThread impl_thread(proxy_);
[email protected]206a3922013-05-17 06:34:121053 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271054 } else {
1055 DCHECK(proxy_->IsImplThread());
[email protected]206a3922013-05-17 06:34:121056 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271057 }
[email protected]d7626ffd2013-03-29 00:17:421058
1059 if (needs_commit)
1060 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141061}
1062
[email protected]f224cc92013-06-06 23:23:321063void LayerTreeHostImpl::SetExternalDrawConstraints(
1064 const gfx::Transform& transform,
1065 gfx::Rect viewport) {
1066 external_transform_ = transform;
1067 external_viewport_ = viewport;
1068}
1069
[email protected]1cd9f5552013-04-26 04:22:031070void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1071 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1072}
1073
[email protected]c8cbae72013-05-23 10:45:031074void LayerTreeHostImpl::BeginFrame(base::TimeTicks frame_time) {
1075 client_->BeginFrameOnImplThread(frame_time);
[email protected]7ed47512013-03-26 22:28:451076}
1077
[email protected]36e5ff12013-06-11 12:19:291078void LayerTreeHostImpl::OnSwapBuffersComplete(
1079 const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271080 // TODO(piman): We may need to do some validation on this ack before
1081 // processing it.
[email protected]36e5ff12013-06-11 12:19:291082 if (ack && renderer_)
1083 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]bb1e2822013-04-17 22:06:011084
[email protected]36e5ff12013-06-11 12:19:291085 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]a46f32932012-12-07 21:43:161086}
1087
[email protected]c1bb5af2013-03-13 19:06:271088void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1089 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291090}
1091
[email protected]c1bb5af2013-03-13 19:06:271092CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1093 CompositorFrameMetadata metadata;
1094 metadata.device_scale_factor = device_scale_factor_;
1095 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1096 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1097 metadata.root_layer_size = active_tree_->ScrollableSize();
1098 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1099 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501100 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271101 if (top_controls_manager_) {
1102 metadata.location_bar_offset =
1103 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1104 metadata.location_bar_content_translation =
1105 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011106 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271107 }
[email protected]bf189f62012-12-18 03:42:111108
[email protected]c1bb5af2013-03-13 19:06:271109 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111110 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271111
[email protected]ffb2720f2013-03-15 19:18:371112 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271113
1114 return metadata;
[email protected]bf189f62012-12-18 03:42:111115}
1116
[email protected]73673592013-04-03 22:14:321117bool LayerTreeHostImpl::AllowPartialSwap() const {
1118 // We don't track damage on the HUD layer (it interacts with damage tracking
1119 // visualizations), so disable partial swaps to make the HUD layer display
1120 // properly.
1121 return !debug_state_.ShowHudRects();
1122}
1123
[email protected]ff1211d2013-06-07 01:58:351124class DidBeginTracingFunctor {
1125 public:
1126 void operator()(LayerImpl* layer) {
1127 layer->DidBeginTracing();
1128 }
1129};
[email protected]37349bc2013-06-04 01:31:521130
[email protected]f0c2a242013-03-15 19:34:521131void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1132 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:271133 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
1134 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201135
1136 if (frame->has_no_damage)
1137 return;
1138
[email protected]c1bb5af2013-03-13 19:06:271139 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141140
[email protected]9e3594522013-03-18 00:57:361141 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:141142
[email protected]372bad5f2013-03-21 16:38:431143 rendering_stats_instrumentation_->SetScreenFrameCount(
1144 fps_counter_->current_frame_number());
1145 rendering_stats_instrumentation_->SetDroppedFrameCount(
1146 fps_counter_->dropped_frame_count());
1147
[email protected]c1bb5af2013-03-13 19:06:271148 if (tile_manager_) {
1149 memory_history_->SaveEntry(
1150 tile_manager_->memory_stats_from_last_assign());
1151 }
[email protected]1191d9d2013-02-02 06:00:331152
[email protected]846f455b2013-03-18 19:07:411153 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021154 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271155 active_tree_->root_layer(),
1156 *frame->render_surface_layer_list,
1157 frame->occluding_screen_space_rects,
1158 frame->non_occluding_screen_space_rects,
1159 debug_state_);
1160 }
[email protected]94f206c12012-08-25 00:09:141161
[email protected]a848c102013-03-26 08:59:091162 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1163 const RenderingStats& stats =
1164 rendering_stats_instrumentation_->GetRenderingStats();
1165 paint_time_counter_->SavePaintTime(stats.total_paint_time);
1166 }
1167
[email protected]37349bc2013-06-04 01:31:521168 bool is_new_trace;
1169 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1170 if (is_new_trace) {
[email protected]ff1211d2013-06-07 01:58:351171 if (pending_tree_)
1172 LayerTreeHostCommon::CallFunctionForSubtree<
1173 DidBeginTracingFunctor, LayerImpl>(
1174 pending_tree_->root_layer());
1175 LayerTreeHostCommon::CallFunctionForSubtree<
1176 DidBeginTracingFunctor, LayerImpl>(
1177 active_tree_->root_layer());
[email protected]37349bc2013-06-04 01:31:521178 }
1179
[email protected]9197dbcb2013-05-15 20:28:511180 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1181 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this,
1182 TracedValue::FromValue(AsValue().release()));
[email protected]131a0c22013-02-12 18:31:081183
[email protected]c1bb5af2013-03-13 19:06:271184 // Because the contents of the HUD depend on everything else in the frame, the
1185 // contents of its texture are updated as the last thing before the frame is
1186 // drawn.
1187 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541188 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141189
[email protected]b09c1942013-05-10 00:06:371190 if (output_surface_->ForcedDrawToSoftwareDevice()) {
1191 scoped_ptr<SoftwareRenderer> temp_software_renderer =
1192 SoftwareRenderer::Create(this, output_surface_.get(), NULL);
1193 temp_software_renderer->DrawFrame(&frame->render_passes);
1194 } else {
1195 renderer_->DrawFrame(&frame->render_passes);
1196 }
[email protected]c1bb5af2013-03-13 19:06:271197 // The render passes should be consumed by the renderer.
1198 DCHECK(frame->render_passes.empty());
1199 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141200
[email protected]c1bb5af2013-03-13 19:06:271201 // The next frame should start by assuming nothing has changed, and changes
1202 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541203 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271204 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1205 DidDrawDamagedArea();
1206 }
1207 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]94f206c12012-08-25 00:09:141208}
1209
[email protected]c1bb5af2013-03-13 19:06:271210void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1211 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1212 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521213
[email protected]c1bb5af2013-03-13 19:06:271214 // Once all layers have been drawn, pending texture uploads should no
1215 // longer block future uploads.
[email protected]fbe89f72013-05-21 07:24:241216 if (resource_provider_)
1217 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141218}
1219
[email protected]c1bb5af2013-03-13 19:06:271220void LayerTreeHostImpl::FinishAllRendering() {
1221 if (renderer_)
1222 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141223}
1224
[email protected]c1bb5af2013-03-13 19:06:271225bool LayerTreeHostImpl::IsContextLost() {
1226 DCHECK(proxy_->IsImplThread());
1227 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141228}
1229
[email protected]c1bb5af2013-03-13 19:06:271230const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1231 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141232}
1233
[email protected]e0341352013-04-06 05:01:201234bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1235 if (frame.has_no_damage)
1236 return false;
[email protected]36e5ff12013-06-11 12:19:291237 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161238 active_tree_->ClearLatencyInfo();
1239 return true;
[email protected]94f206c12012-08-25 00:09:141240}
1241
[email protected]c8cbae72013-05-23 10:45:031242void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451243 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031244 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451245}
1246
[email protected]94c40e6292013-05-24 22:01:501247float LayerTreeHostImpl::DeviceScaleFactor() const {
1248 return device_scale_factor_;
1249}
1250
[email protected]ffb2720f2013-03-15 19:18:371251gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
1252 gfx::SizeF dip_size =
[email protected]f224cc92013-06-06 23:23:321253 gfx::ScaleSize(device_viewport_size(), 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371254
1255 // The clip layer should be used if non-overlay scrollbars may exist since
1256 // it adjusts for them.
1257 LayerImpl* clip_layer = active_tree_->RootClipLayer();
[email protected]8e0176d2013-03-21 03:14:521258 if (!Settings().solid_color_scrollbars && clip_layer &&
[email protected]ffb2720f2013-03-15 19:18:371259 clip_layer->masks_to_bounds())
1260 dip_size = clip_layer->bounds();
1261
[email protected]ed511b8d2013-03-25 03:29:291262 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371263 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401264 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291265 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371266}
1267
[email protected]c1bb5af2013-03-13 19:06:271268const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1269 return settings();
[email protected]493067512012-09-19 23:34:101270}
1271
[email protected]c1bb5af2013-03-13 19:06:271272void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011273 // TODO(jamesr): The renderer_ check is needed to make some of the
1274 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1275 // important) in production. We should adjust the test to not need this.
1276 if (renderer_)
1277 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141278}
1279
[email protected]c1bb5af2013-03-13 19:06:271280void LayerTreeHostImpl::Readback(void* pixels,
1281 gfx::Rect rect_in_device_viewport) {
1282 DCHECK(renderer_);
1283 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141284}
1285
[email protected]59adb112013-04-09 04:48:441286bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341287 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011288}
1289
[email protected]c1bb5af2013-03-13 19:06:271290LayerImpl* LayerTreeHostImpl::RootLayer() const {
1291 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081292}
1293
[email protected]c1bb5af2013-03-13 19:06:271294LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1295 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081296}
1297
[email protected]c1bb5af2013-03-13 19:06:271298LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1299 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081300}
1301
[email protected]94f206c12012-08-25 00:09:141302// Content layers can be either directly scrollable or contained in an outer
1303// scrolling layer which applies the scroll transform. Given a content layer,
1304// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271305static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1306 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141307 return 0;
[email protected]c1bb5af2013-03-13 19:06:271308
1309 if (layer_impl->scrollable())
1310 return layer_impl;
1311
1312 if (layer_impl->DrawsContent() &&
1313 layer_impl->parent() &&
1314 layer_impl->parent()->scrollable())
1315 return layer_impl->parent();
1316
1317 return 0;
[email protected]94f206c12012-08-25 00:09:141318}
1319
[email protected]c1bb5af2013-03-13 19:06:271320void LayerTreeHostImpl::CreatePendingTree() {
1321 CHECK(!pending_tree_);
1322 if (recycle_tree_)
1323 recycle_tree_.swap(pending_tree_);
1324 else
1325 pending_tree_ = LayerTreeImpl::create(this);
1326 client_->OnCanDrawStateChanged(CanDraw());
1327 client_->OnHasPendingTreeStateChanged(pending_tree_);
1328 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1329 TRACE_EVENT_ASYNC_STEP0("cc",
1330 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271331}
1332
[email protected]c1bb5af2013-03-13 19:06:271333void LayerTreeHostImpl::CheckForCompletedTileUploads() {
1334 DCHECK(!client_->IsInsideDraw()) <<
1335 "Checking for completed uploads within a draw may trigger "
1336 "spurious redraws.";
1337 if (tile_manager_)
1338 tile_manager_->CheckForCompletedTileUploads();
[email protected]eabe5002013-01-12 22:07:481339}
1340
[email protected]94bf75c2013-06-12 13:20:041341void LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1342 DCHECK(pending_tree_);
[email protected]fbe89f72013-05-21 07:24:241343 CHECK(settings_.impl_side_painting);
[email protected]2ae038b2013-01-28 12:52:091344
[email protected]94bf75c2013-06-12 13:20:041345 // This call may activate the tree.
1346 CheckForCompletedTileUploads();
1347 if (!pending_tree_)
1348 return;
1349
1350 // The tile manager is usually responsible for notifying activation.
1351 // If there is no tile manager, then we need to manually activate.
1352 if (!tile_manager_ || tile_manager_->AreTilesRequiredForActivationReady()) {
1353 ActivatePendingTree();
1354 return;
1355 }
[email protected]615c78a2013-01-24 23:44:161356
[email protected]f6742f52013-05-08 23:52:221357 TRACE_EVENT_ASYNC_STEP1(
[email protected]fcb846d2013-05-22 01:42:361358 "cc",
1359 "PendingTree", pending_tree_.get(), "activate",
1360 "state", TracedValue::FromValue(ActivationStateAsValue().release()));
[email protected]2e7ca422012-12-20 02:57:271361}
1362
[email protected]c1bb5af2013-03-13 19:06:271363void LayerTreeHostImpl::ActivatePendingTree() {
1364 CHECK(pending_tree_);
1365 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351366
[email protected]1960a712013-04-30 17:06:471367 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271368 active_tree_->PushPersistedState(pending_tree_.get());
1369 if (pending_tree_->needs_full_tree_sync()) {
1370 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331371 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271372 active_tree_->DetachLayerTree(),
1373 active_tree_.get()));
1374 }
[email protected]b5651c22013-03-14 15:06:331375 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271376 active_tree_->root_layer());
1377 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511378
[email protected]c1bb5af2013-03-13 19:06:271379 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511380
[email protected]c1bb5af2013-03-13 19:06:271381 // Now that we've synced everything from the pending tree to the active
1382 // tree, rename the pending tree the recycle tree so we can reuse it on the
1383 // next sync.
1384 pending_tree_.swap(recycle_tree_);
1385 recycle_tree_->ClearRenderSurfaces();
[email protected]48871fc2013-01-23 07:36:511386
[email protected]1960a712013-04-30 17:06:471387 active_tree_->SetRootLayerScrollOffsetDelegate(
1388 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271389 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221390
[email protected]c1bb5af2013-03-13 19:06:271391 // Reduce wasted memory now that unlinked resources are guaranteed not
1392 // to be used.
1393 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411394
[email protected]c1bb5af2013-03-13 19:06:271395 client_->OnCanDrawStateChanged(CanDraw());
1396 client_->OnHasPendingTreeStateChanged(pending_tree_);
1397 client_->SetNeedsRedrawOnImplThread();
1398 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241399
[email protected]a848c102013-03-26 08:59:091400 if (debug_state_.continuous_painting) {
1401 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431402 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091403 paint_time_counter_->SavePaintTime(
[email protected]41abb6d92013-04-26 16:38:501404 stats.total_paint_time + stats.total_record_time +
[email protected]a848c102013-03-26 08:59:091405 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271406 }
[email protected]2a61ad52013-05-13 14:01:291407
1408 client_->DidActivatePendingTree();
[email protected]2e7ca422012-12-20 02:57:271409}
1410
[email protected]c1bb5af2013-03-13 19:06:271411void LayerTreeHostImpl::SetVisible(bool visible) {
1412 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141413
[email protected]c1bb5af2013-03-13 19:06:271414 if (visible_ == visible)
1415 return;
1416 visible_ = visible;
1417 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121418 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141419
[email protected]c1bb5af2013-03-13 19:06:271420 if (!renderer_)
1421 return;
[email protected]94f206c12012-08-25 00:09:141422
[email protected]c1bb5af2013-03-13 19:06:271423 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141424}
1425
[email protected]206a3922013-05-17 06:34:121426ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
1427 if (!debug_state_.rasterize_only_visible_content)
1428 return managed_memory_policy_;
1429
1430 ManagedMemoryPolicy actual = managed_memory_policy_;
1431 actual.priority_cutoff_when_not_visible =
1432 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1433 actual.priority_cutoff_when_visible =
1434 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1435 return actual;
1436}
1437
[email protected]c1bb5af2013-03-13 19:06:271438bool LayerTreeHostImpl::InitializeRenderer(
1439 scoped_ptr<OutputSurface> output_surface) {
1440 // Since we will create a new resource provider, we cannot continue to use
1441 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1442 // before we destroy the old resource provider.
1443 if (active_tree_->root_layer())
1444 ClearRenderSurfaces();
[email protected]ff1211d2013-06-07 01:58:351445 if (active_tree_->root_layer())
1446 SendDidLoseOutputSurfaceRecursive(active_tree_->root_layer());
1447 if (pending_tree_ && pending_tree_->root_layer())
1448 SendDidLoseOutputSurfaceRecursive(pending_tree_->root_layer());
1449 if (recycle_tree_ && recycle_tree_->root_layer())
1450 SendDidLoseOutputSurfaceRecursive(recycle_tree_->root_layer());
[email protected]7ba3ca72013-04-11 06:37:251451 if (resource_provider_)
1452 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401453
[email protected]c1bb5af2013-03-13 19:06:271454 // Note: order is important here.
1455 renderer_.reset();
1456 tile_manager_.reset();
1457 resource_provider_.reset();
1458 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141459
[email protected]c1bb5af2013-03-13 19:06:271460 if (!output_surface->BindToClient(this))
1461 return false;
[email protected]be3181652012-09-25 13:02:131462
[email protected]2b154b22013-06-07 09:03:271463 return DoInitializeRenderer(output_surface.Pass(),
1464 false /* is_deferred_init */);
1465}
1466
1467bool LayerTreeHostImpl::DoInitializeRenderer(
1468 scoped_ptr<OutputSurface> output_surface,
1469 bool is_deferred_init) {
1470 if (output_surface->capabilities().deferred_gl_initialization &&
1471 !is_deferred_init) {
[email protected]fbe89f72013-05-21 07:24:241472 // TODO(joth): Defer creating the Renderer too, until GL is initialized.
1473 // See http://crbug.com/230197
1474 renderer_ = SoftwareRenderer::Create(this, output_surface.get(), NULL);
1475 } else {
1476 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1477 output_surface.get(), settings_.highp_threshold_min);
1478 if (!resource_provider)
1479 return false;
[email protected]be3181652012-09-25 13:02:131480
[email protected]36e5ff12013-06-11 12:19:291481 if (output_surface->capabilities().delegated_rendering) {
[email protected]fbe89f72013-05-21 07:24:241482 renderer_ = DelegatingRenderer::Create(this, output_surface.get(),
1483 resource_provider.get());
1484 } else if (output_surface->context3d()) {
1485 renderer_ = GLRenderer::Create(this,
1486 output_surface.get(),
1487 resource_provider.get(),
[email protected]bec084292013-05-21 21:31:441488 settings_.highp_threshold_min,
1489 settings_.force_direct_layer_drawing);
[email protected]fbe89f72013-05-21 07:24:241490 } else if (output_surface->software_device()) {
1491 renderer_ = SoftwareRenderer::Create(this,
1492 output_surface.get(),
[email protected]c1bb5af2013-03-13 19:06:271493 resource_provider.get());
[email protected]fbe89f72013-05-21 07:24:241494 }
1495 if (!renderer_)
1496 return false;
[email protected]be3181652012-09-25 13:02:131497
[email protected]6123930c2013-05-23 06:48:371498 if (settings_.impl_side_painting) {
1499 bool using_map_image = GetRendererCapabilities().using_map_image;
1500 tile_manager_ = TileManager::Create(this,
1501 resource_provider.get(),
1502 settings_.num_raster_threads,
1503 settings_.use_color_estimator,
1504 rendering_stats_instrumentation_,
1505 using_map_image);
1506 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
1507 }
1508
[email protected]fbe89f72013-05-21 07:24:241509 resource_provider_ = resource_provider.Pass();
1510 }
1511
[email protected]049fc7a2013-06-18 12:32:351512 // Setup BeginFrameEmulation if it's not supported natively
1513 if (!settings_.begin_frame_scheduling_enabled) {
1514 const base::TimeDelta display_refresh_interval =
1515 base::TimeDelta::FromMicroseconds(
1516 base::Time::kMicrosecondsPerSecond /
1517 settings_.refresh_rate);
1518
1519 output_surface->InitializeBeginFrameEmulation(
1520 proxy_->ImplThread(),
1521 settings_.throttle_frame_production,
1522 display_refresh_interval);
1523 }
1524
1525 int max_frames_pending =
1526 output_surface->capabilities().max_frames_pending;
1527 if (max_frames_pending <= 0)
1528 max_frames_pending = FrameRateController::DEFAULT_MAX_FRAMES_PENDING;
1529 output_surface->SetMaxFramesPending(max_frames_pending);
1530
[email protected]c1bb5af2013-03-13 19:06:271531 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141532
[email protected]c1bb5af2013-03-13 19:06:271533 if (!visible_)
1534 renderer_->SetVisible(visible_);
[email protected]94f206c12012-08-25 00:09:141535
[email protected]c1bb5af2013-03-13 19:06:271536 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211537
[email protected]c1bb5af2013-03-13 19:06:271538 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1539 // to be initialized to get max texture size.
1540 active_tree_->set_needs_update_draw_properties();
1541 if (pending_tree_)
1542 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161543
[email protected]c1bb5af2013-03-13 19:06:271544 return true;
[email protected]94f206c12012-08-25 00:09:141545}
1546
[email protected]2b154b22013-06-07 09:03:271547bool LayerTreeHostImpl::DeferredInitialize(
1548 scoped_refptr<ContextProvider> offscreen_context_provider) {
1549 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1550 DCHECK(output_surface_->context3d());
1551
1552 // TODO(boliu): This is temporary until proper resource clean up is possible
1553 // without resetting |tile_manager_| or |resource_provider_|.
1554 DCHECK(!resource_provider_);
1555
1556 bool success =
1557 DoInitializeRenderer(output_surface_.Pass(), true /* is_deferred_init */);
1558 client_->DidTryInitializeRendererOnImplThread(success,
1559 offscreen_context_provider);
1560 return success;
1561}
1562
[email protected]18ce59702013-04-09 04:58:401563void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1564 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271565 return;
[email protected]94f206c12012-08-25 00:09:141566
[email protected]c1bb5af2013-03-13 19:06:271567 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1568 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271569
[email protected]c1bb5af2013-03-13 19:06:271570 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141571
[email protected]c1bb5af2013-03-13 19:06:271572 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141573
[email protected]c1bb5af2013-03-13 19:06:271574 if (renderer_)
1575 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211576
[email protected]c1bb5af2013-03-13 19:06:271577 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441578 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141579}
1580
[email protected]d9083762013-03-24 01:36:401581void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1582 if (overdraw_bottom_height == overdraw_bottom_height_)
1583 return;
1584 overdraw_bottom_height_ = overdraw_bottom_height;
1585
1586 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441587 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401588}
1589
[email protected]c1bb5af2013-03-13 19:06:271590void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1591 if (device_scale_factor == device_scale_factor_)
1592 return;
1593 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271594
[email protected]94c40e6292013-05-24 22:01:501595 if (renderer_)
1596 renderer_->ViewportChanged();
1597
[email protected]c1bb5af2013-03-13 19:06:271598 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441599 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141600}
1601
[email protected]f224cc92013-06-06 23:23:321602gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1603 if (external_viewport_.IsEmpty())
1604 return gfx::Rect(device_viewport_size_);
1605
1606 return external_viewport_;
1607}
1608
1609const gfx::Transform& LayerTreeHostImpl::DeviceTransform() const {
1610 return external_transform_;
1611}
1612
[email protected]c1bb5af2013-03-13 19:06:271613void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1614 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141615}
1616
[email protected]59adb112013-04-09 04:48:441617void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271618 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441619 active_tree_->set_needs_update_draw_properties();
1620 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141621}
1622
[email protected]c1bb5af2013-03-13 19:06:271623bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041624 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341625 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141626}
1627
[email protected]200a9c062013-05-20 04:34:371628void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1629 DCHECK(input_handler_client_ == NULL);
1630 input_handler_client_ = client;
1631}
1632
[email protected]5ff3c9782013-04-29 17:35:121633InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1634 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291635 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141636
[email protected]c1bb5af2013-03-13 19:06:271637 if (top_controls_manager_)
1638 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381639
[email protected]c1bb5af2013-03-13 19:06:271640 DCHECK(!CurrentlyScrollingLayer());
1641 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141642
[email protected]c1bb5af2013-03-13 19:06:271643 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141644 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141645
[email protected]c1bb5af2013-03-13 19:06:271646 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1647 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141648
[email protected]c1bb5af2013-03-13 19:06:271649 // First find out which layer was hit from the saved list of visible layers
1650 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391651 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271652 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521653
[email protected]c1bb5af2013-03-13 19:06:271654 // Walk up the hierarchy and look for a scrollable layer.
1655 LayerImpl* potentially_scrolling_layer_impl = 0;
1656 for (; layer_impl; layer_impl = layer_impl->parent()) {
1657 // The content layer can also block attempts to scroll outside the main
1658 // thread.
1659 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1660 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431661 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271662 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1663 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141664 }
1665
[email protected]c1bb5af2013-03-13 19:06:271666 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1667 if (!scroll_layer_impl)
1668 continue;
[email protected]94f206c12012-08-25 00:09:141669
[email protected]c1bb5af2013-03-13 19:06:271670 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141671
[email protected]c1bb5af2013-03-13 19:06:271672 // If any layer wants to divert the scroll event to the main thread, abort.
1673 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431674 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271675 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1676 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141677 }
1678
[email protected]c1bb5af2013-03-13 19:06:271679 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1680 potentially_scrolling_layer_impl = scroll_layer_impl;
1681 }
1682
1683 // When hiding top controls is enabled and the controls are hidden or
1684 // overlaying the content, force scrolls to be enabled on the root layer to
1685 // allow bringing the top controls back into view.
1686 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1687 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521688 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271689 potentially_scrolling_layer_impl = RootScrollLayer();
1690 }
1691
1692 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201693 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271694 potentially_scrolling_layer_impl);
1695 should_bubble_scrolls_ = (type != NonBubblingGesture);
1696 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431697 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271698 client_->RenewTreePriority();
1699 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
1700 return ScrollStarted;
1701 }
1702 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141703}
1704
[email protected]c1bb5af2013-03-13 19:06:271705gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1706 LayerImpl* layer_impl,
1707 float scale_from_viewport_to_screen_space,
1708 gfx::PointF viewport_point,
1709 gfx::Vector2dF viewport_delta) {
1710 // Layers with non-invertible screen space transforms should not have passed
1711 // the scroll hit test in the first place.
1712 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1713 gfx::Transform inverse_screen_space_transform(
1714 gfx::Transform::kSkipInitialization);
1715 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1716 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351717 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1718 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271719 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141720
[email protected]c1bb5af2013-03-13 19:06:271721 gfx::PointF screen_space_point =
1722 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141723
[email protected]c1bb5af2013-03-13 19:06:271724 gfx::Vector2dF screen_space_delta = viewport_delta;
1725 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1726
1727 // First project the scroll start and end points to local layer space to find
1728 // the scroll delta in layer coordinates.
1729 bool start_clipped, end_clipped;
1730 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1731 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211732 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271733 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211734 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271735 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211736 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271737 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211738 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271739
1740 // In general scroll point coordinates should not get clipped.
1741 DCHECK(!start_clipped);
1742 DCHECK(!end_clipped);
1743 if (start_clipped || end_clipped)
1744 return gfx::Vector2dF();
1745
1746 // local_start_point and local_end_point are in content space but we want to
1747 // move them to layer space for scrolling.
1748 float width_scale = 1.f / layer_impl->contents_scale_x();
1749 float height_scale = 1.f / layer_impl->contents_scale_y();
1750 local_start_point.Scale(width_scale, height_scale);
1751 local_end_point.Scale(width_scale, height_scale);
1752
1753 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:471754 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:271755 layer_impl->ScrollBy(local_end_point - local_start_point);
1756
1757 // Get the end point in the layer's content space so we can apply its
1758 // ScreenSpaceTransform.
1759 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:471760 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:271761 previous_delta;
1762 gfx::PointF actual_local_content_end_point =
1763 gfx::ScalePoint(actual_local_end_point,
1764 1.f / width_scale,
1765 1.f / height_scale);
1766
1767 // Calculate the applied scroll delta in viewport space coordinates.
1768 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211769 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271770 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211771 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271772 DCHECK(!end_clipped);
1773 if (end_clipped)
1774 return gfx::Vector2dF();
1775 gfx::PointF actual_viewport_end_point =
1776 gfx::ScalePoint(actual_screen_space_end_point,
1777 1.f / scale_from_viewport_to_screen_space);
1778 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141779}
1780
[email protected]c1bb5af2013-03-13 19:06:271781static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1782 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:471783 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271784 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:471785 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:271786}
1787
1788bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1789 gfx::Vector2dF scroll_delta) {
1790 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1791 if (!CurrentlyScrollingLayer())
1792 return false;
1793
1794 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:531795 gfx::Vector2dF unused_root_delta;
[email protected]c1bb5af2013-03-13 19:06:271796 bool did_scroll = false;
[email protected]60b4d252013-03-23 18:49:421797 bool consume_by_top_controls = top_controls_manager_ &&
1798 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061799
[email protected]c1bb5af2013-03-13 19:06:271800 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1801 layer_impl;
1802 layer_impl = layer_impl->parent()) {
1803 if (!layer_impl->scrollable())
1804 continue;
1805
[email protected]a2b5ded2013-05-20 21:32:531806 if (layer_impl == RootScrollLayer()) {
1807 // Only allow bubble scrolling when the scroll is in the direction to make
1808 // the top controls visible.
1809 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1810 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1811 UpdateMaxScrollOffset();
1812 }
1813 // Track root layer deltas for reporting overscroll.
1814 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:421815 }
1816
[email protected]c1bb5af2013-03-13 19:06:271817 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271818 // Gesture events need to be transformed from viewport coordinates to local
1819 // layer coordinates so that the scrolling contents exactly follow the
1820 // user's finger. In contrast, wheel events represent a fixed amount of
1821 // scrolling so we can just apply them directly.
1822 if (!wheel_scrolling_) {
1823 float scale_from_viewport_to_screen_space = device_scale_factor_;
1824 applied_delta =
1825 ScrollLayerWithViewportSpaceDelta(layer_impl,
1826 scale_from_viewport_to_screen_space,
1827 viewport_point, pending_delta);
1828 } else {
1829 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141830 }
[email protected]94f206c12012-08-25 00:09:141831
[email protected]c1bb5af2013-03-13 19:06:271832 // If the layer wasn't able to move, try the next one in the hierarchy.
1833 float move_threshold_squared = 0.1f * 0.1f;
1834 if (applied_delta.LengthSquared() < move_threshold_squared) {
1835 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1836 continue;
1837 else
1838 break;
[email protected]94f206c12012-08-25 00:09:141839 }
[email protected]a2b5ded2013-05-20 21:32:531840
1841 if (layer_impl == RootScrollLayer())
1842 unused_root_delta.Subtract(applied_delta);
1843
[email protected]c1bb5af2013-03-13 19:06:271844 did_scroll = true;
1845 did_lock_scrolling_layer_ = true;
1846 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:201847 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:271848 break;
[email protected]94f206c12012-08-25 00:09:141849 }
[email protected]94f206c12012-08-25 00:09:141850
[email protected]c1bb5af2013-03-13 19:06:271851 // If the applied delta is within 45 degrees of the input delta, bail out to
1852 // make it easier to scroll just one layer in one direction without
1853 // affecting any of its parents.
1854 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:211855 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:271856 applied_delta, pending_delta) < angle_threshold) {
1857 pending_delta = gfx::Vector2d();
1858 break;
[email protected]4a23c374c2012-12-08 08:38:551859 }
[email protected]c1bb5af2013-03-13 19:06:271860
1861 // Allow further movement only on an axis perpendicular to the direction in
1862 // which the layer moved.
1863 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:211864 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:271865
[email protected]a2b5ded2013-05-20 21:32:531866 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:271867 break;
1868 }
1869
1870 if (did_scroll) {
1871 client_->SetNeedsCommitOnImplThread();
1872 client_->SetNeedsRedrawOnImplThread();
1873 client_->RenewTreePriority();
[email protected]a2b5ded2013-05-20 21:32:531874
1875 // Scrolling of any layer will reset root overscroll accumulation.
1876 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]c1bb5af2013-03-13 19:06:271877 }
[email protected]a2b5ded2013-05-20 21:32:531878
1879 accumulated_root_overscroll_ += unused_root_delta;
1880 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
1881 if (did_overscroll && input_handler_client_) {
1882 input_handler_client_->DidOverscroll(accumulated_root_overscroll_,
1883 current_fling_velocity_);
1884 }
1885
[email protected]c1bb5af2013-03-13 19:06:271886 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:551887}
1888
[email protected]be782f52013-03-23 21:36:141889// This implements scrolling by page as described here:
1890// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
1891// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:491892bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
1893 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:141894 DCHECK(wheel_scrolling_);
1895
1896 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1897 layer_impl;
1898 layer_impl = layer_impl->parent()) {
1899 if (!layer_impl->scrollable())
1900 continue;
1901
1902 if (!layer_impl->vertical_scrollbar_layer())
1903 continue;
1904
1905 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
1906
1907 // These magical values match WebKit and are designed to scroll nearly the
1908 // entire visible content height but leave a bit of overlap.
1909 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:491910 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:141911 page = -page;
1912
1913 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
1914
1915 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
1916
1917 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:141918 client_->SetNeedsCommitOnImplThread();
1919 client_->SetNeedsRedrawOnImplThread();
1920 client_->RenewTreePriority();
1921 return true;
1922 }
1923
1924 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
1925 }
1926
1927 return false;
1928}
1929
[email protected]1960a712013-04-30 17:06:471930void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
1931 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
1932 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
1933 active_tree_->SetRootLayerScrollOffsetDelegate(
1934 root_layer_scroll_offset_delegate_);
1935}
1936
1937void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
1938 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
1939 client_->SetNeedsCommitOnImplThread();
1940}
1941
[email protected]c1bb5af2013-03-13 19:06:271942void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
1943 active_tree_->ClearCurrentlyScrollingLayer();
1944 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:531945 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:201946 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:141947}
1948
[email protected]c1bb5af2013-03-13 19:06:271949void LayerTreeHostImpl::ScrollEnd() {
1950 if (top_controls_manager_)
1951 top_controls_manager_->ScrollEnd();
1952 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:051953 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:141954}
1955
[email protected]5ff3c9782013-04-29 17:35:121956InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]7c45d8152013-04-23 18:27:211957 if (active_tree_->CurrentlyScrollingLayer())
1958 return ScrollStarted;
1959
1960 return ScrollIgnored;
1961}
1962
[email protected]6e921bd2013-04-29 21:10:201963void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
1964 current_fling_velocity_ = velocity;
1965}
1966
[email protected]c1bb5af2013-03-13 19:06:271967void LayerTreeHostImpl::PinchGestureBegin() {
1968 pinch_gesture_active_ = true;
1969 previous_pinch_anchor_ = gfx::Point();
1970 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:141971}
1972
[email protected]c1bb5af2013-03-13 19:06:271973void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
1974 gfx::Point anchor) {
1975 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:281976
[email protected]c1bb5af2013-03-13 19:06:271977 if (!RootScrollLayer())
1978 return;
[email protected]d3afa112012-12-08 06:24:281979
[email protected]c1bb5af2013-03-13 19:06:271980 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1981 // position over the course of the magnify.
1982 float page_scale_delta = active_tree_->page_scale_delta();
1983 gfx::PointF previous_scale_anchor =
1984 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1985 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
1986 page_scale_delta = active_tree_->page_scale_delta();
1987 gfx::PointF new_scale_anchor =
1988 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1989 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
1990
1991 previous_pinch_anchor_ = anchor;
1992
1993 move.Scale(1 / active_tree_->page_scale_factor());
1994
1995 RootScrollLayer()->ScrollBy(move);
1996
[email protected]c1bb5af2013-03-13 19:06:271997 client_->SetNeedsCommitOnImplThread();
1998 client_->SetNeedsRedrawOnImplThread();
1999 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282000}
2001
[email protected]c1bb5af2013-03-13 19:06:272002void LayerTreeHostImpl::PinchGestureEnd() {
2003 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:272004 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142005}
2006
[email protected]c1bb5af2013-03-13 19:06:272007static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2008 LayerImpl* layer_impl) {
2009 if (!layer_impl)
2010 return;
[email protected]94f206c12012-08-25 00:09:142011
[email protected]c1bb5af2013-03-13 19:06:272012 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472013 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272014 if (!scroll_delta.IsZero()) {
2015 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392016 scroll.layer_id = layer_impl->id();
2017 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272018 scroll_info->scrolls.push_back(scroll);
2019 layer_impl->SetSentScrollDelta(scroll_delta);
2020 }
[email protected]94f206c12012-08-25 00:09:142021
[email protected]c1bb5af2013-03-13 19:06:272022 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2023 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142024}
2025
[email protected]c1bb5af2013-03-13 19:06:272026scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2027 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302028
[email protected]c1bb5af2013-03-13 19:06:272029 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392030 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2031 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302032
[email protected]c1bb5af2013-03-13 19:06:272033 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302034}
2035
[email protected]c1bb5af2013-03-13 19:06:272036void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]878705be2013-04-15 22:44:022037 SetViewportDamage(gfx::Rect(device_viewport_size_));
[email protected]829ad972013-01-28 23:36:102038}
2039
[email protected]c1bb5af2013-03-13 19:06:272040void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2041 if (!page_scale_animation_ || !RootScrollLayer())
2042 return;
2043
2044 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2045 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472046 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272047
2048 active_tree_->SetPageScaleDelta(
2049 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2050 active_tree_->page_scale_factor());
2051 gfx::Vector2dF next_scroll =
2052 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2053
2054 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2055 client_->SetNeedsRedrawOnImplThread();
2056
2057 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2058 page_scale_animation_.reset();
2059 client_->SetNeedsCommitOnImplThread();
2060 client_->RenewTreePriority();
2061 }
[email protected]829ad972013-01-28 23:36:102062}
2063
[email protected]ffb2720f2013-03-15 19:18:372064void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2065 if (!top_controls_manager_ || !RootScrollLayer())
2066 return;
2067 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2068 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172069 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2070 return;
[email protected]ffb2720f2013-03-15 19:18:372071 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2072 scroll, 1.f / active_tree_->total_page_scale_factor()));
2073}
2074
[email protected]c1bb5af2013-03-13 19:06:272075void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2076 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522077 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272078 animation_registrar_->active_animation_controllers().empty() ||
2079 !active_tree_->root_layer())
2080 return;
2081
2082 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2083
2084 last_animation_time_ = wall_clock_time;
2085 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2086
2087 AnimationRegistrar::AnimationControllerMap copy =
2088 animation_registrar_->active_animation_controllers();
2089 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2090 iter != copy.end();
2091 ++iter)
2092 (*iter).second->Animate(monotonic_seconds);
2093
2094 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082095}
2096
[email protected]3d9f7432013-04-06 00:35:182097void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522098 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272099 animation_registrar_->active_animation_controllers().empty() ||
2100 !active_tree_->root_layer())
2101 return;
2102
2103 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2104 scoped_ptr<AnimationEventsVector> events =
2105 make_scoped_ptr(new AnimationEventsVector);
2106 AnimationRegistrar::AnimationControllerMap copy =
2107 animation_registrar_->active_animation_controllers();
2108 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2109 iter != copy.end();
2110 ++iter)
[email protected]3d9f7432013-04-06 00:35:182111 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272112
2113 if (!events->empty()) {
2114 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2115 last_animation_time_);
2116 }
[email protected]131a0c22013-02-12 18:31:082117}
2118
[email protected]c1bb5af2013-03-13 19:06:272119base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2120 return base::TimeDelta::FromSeconds(1);
2121}
2122
[email protected]ff1211d2013-06-07 01:58:352123void LayerTreeHostImpl::SendDidLoseOutputSurfaceRecursive(LayerImpl* current) {
2124 DCHECK(current);
2125 current->DidLoseOutputSurface();
2126 if (current->mask_layer())
2127 SendDidLoseOutputSurfaceRecursive(current->mask_layer());
2128 if (current->replica_layer())
2129 SendDidLoseOutputSurfaceRecursive(current->replica_layer());
2130 for (size_t i = 0; i < current->children().size(); ++i)
2131 SendDidLoseOutputSurfaceRecursive(current->children()[i]);
2132}
2133
[email protected]c1bb5af2013-03-13 19:06:272134void LayerTreeHostImpl::ClearRenderSurfaces() {
2135 active_tree_->ClearRenderSurfaces();
2136 if (pending_tree_)
2137 pending_tree_->ClearRenderSurfaces();
2138}
2139
2140std::string LayerTreeHostImpl::LayerTreeAsText() const {
2141 std::string str;
2142 if (active_tree_->root_layer()) {
2143 str = active_tree_->root_layer()->LayerTreeAsText();
2144 str += "RenderSurfaces:\n";
2145 DumpRenderSurfaces(&str, 1, active_tree_->root_layer());
2146 }
2147 return str;
2148}
2149
2150std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2151 std::string str;
2152 if (active_tree_->root_layer()) {
2153 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2154 base::JSONWriter::WriteWithOptions(
2155 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2156 }
2157 return str;
2158}
2159
2160void LayerTreeHostImpl::DumpRenderSurfaces(std::string* str,
2161 int indent,
2162 const LayerImpl* layer) const {
2163 if (layer->render_surface())
2164 layer->render_surface()->DumpSurface(str, indent);
2165
2166 for (size_t i = 0; i < layer->children().size(); ++i)
2167 DumpRenderSurfaces(str, indent, layer->children()[i]);
2168}
2169
2170int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362171 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272172}
2173
[email protected]c1bb5af2013-03-13 19:06:272174void LayerTreeHostImpl::SendManagedMemoryStats(
2175 size_t memory_visible_bytes,
2176 size_t memory_visible_and_nearby_bytes,
2177 size_t memory_use_bytes) {
2178 if (!renderer_)
2179 return;
2180
2181 // Round the numbers being sent up to the next 8MB, to throttle the rate
2182 // at which we spam the GPU process.
2183 static const size_t rounding_step = 8 * 1024 * 1024;
2184 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2185 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2186 rounding_step);
2187 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2188 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2189 last_sent_memory_visible_and_nearby_bytes_ ==
2190 memory_visible_and_nearby_bytes &&
2191 last_sent_memory_use_bytes_ == memory_use_bytes) {
2192 return;
2193 }
2194 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2195 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2196 last_sent_memory_use_bytes_ = memory_use_bytes;
2197
2198 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2199 last_sent_memory_visible_and_nearby_bytes_,
2200 last_sent_memory_use_bytes_);
2201}
2202
2203void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2204 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2205}
2206
2207void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2208 base::TimeTicks time) {
2209 if (!layer)
2210 return;
2211
2212 ScrollbarAnimationController* scrollbar_controller =
2213 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352214 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202215 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422216 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2217 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272218 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202219 }
[email protected]c1bb5af2013-03-13 19:06:272220
2221 for (size_t i = 0; i < layer->children().size(); ++i)
2222 AnimateScrollbarsRecursive(layer->children()[i], time);
2223}
2224
[email protected]21c9dee72013-06-15 01:20:052225void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202226 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052227 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202228}
2229
2230void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2231 base::TimeTicks time) {
2232 if (!layer)
2233 return;
2234
2235 ScrollbarAnimationController* scrollbar_controller =
2236 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352237 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2238 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202239 if (delay > base::TimeDelta())
2240 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352241 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202242 client_->SetNeedsRedrawOnImplThread();
2243 }
2244
2245 for (size_t i = 0; i < layer->children().size(); ++i)
2246 StartScrollbarAnimationRecursive(layer->children()[i], time);
2247}
2248
[email protected]c1bb5af2013-03-13 19:06:272249void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2250 if (!tile_manager_)
2251 return;
2252
2253 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2254 if (new_state.tree_priority == priority)
2255 return;
2256
2257 new_state.tree_priority = priority;
2258 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:262259 manage_tiles_needed_ = true;
[email protected]c1bb5af2013-03-13 19:06:272260}
2261
[email protected]8347d692013-05-17 23:22:382262void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552263 current_frame_timeticks_ = base::TimeTicks();
2264 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272265}
2266
[email protected]21c9dee72013-06-15 01:20:052267void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2268 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552269 if (ticks->is_null()) {
2270 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052271 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552272 *now = base::Time::Now();
2273 }
2274}
2275
2276base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2277 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2278 return current_frame_timeticks_;
2279}
2280
2281base::Time LayerTreeHostImpl::CurrentFrameTime() {
2282 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272283 return current_frame_time_;
2284}
2285
[email protected]21c9dee72013-06-15 01:20:052286base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
2287 return base::TimeTicks::Now();
2288}
2289
[email protected]c1bb5af2013-03-13 19:06:272290scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
2291 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222292 if (this->pending_tree_)
2293 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272294 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212295 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272296 if (tile_manager_)
2297 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2298 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222299 if (pending_tree_)
2300 state->Set("pending_tree", pending_tree_->AsValue().release());
2301 return state.PassAs<base::Value>();
2302}
2303
2304scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222305 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2306 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472307 if (tile_manager_)
2308 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272309 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082310}
2311
[email protected]b9dcf43a2013-01-09 00:15:292312// static
[email protected]c1bb5af2013-03-13 19:06:272313LayerImpl* LayerTreeHostImpl::GetNonCompositedContentLayerRecursive(
2314 LayerImpl* layer) {
2315 if (!layer)
[email protected]b9dcf43a2013-01-09 00:15:292316 return NULL;
[email protected]c1bb5af2013-03-13 19:06:272317
2318 if (layer->DrawsContent())
2319 return layer;
2320
[email protected]50761e92013-03-29 20:51:282321 for (LayerImplList::const_iterator it = layer->children().begin();
[email protected]c1bb5af2013-03-13 19:06:272322 it != layer->children().end(); ++it) {
2323 LayerImpl* nccr = GetNonCompositedContentLayerRecursive(*it);
2324 if (nccr)
2325 return nccr;
2326 }
2327
2328 return NULL;
[email protected]b9dcf43a2013-01-09 00:15:292329}
2330
[email protected]c1bb5af2013-03-13 19:06:272331skia::RefPtr<SkPicture> LayerTreeHostImpl::CapturePicture() {
2332 LayerTreeImpl* tree =
2333 pending_tree_ ? pending_tree_.get() : active_tree_.get();
2334 LayerImpl* layer = GetNonCompositedContentLayerRecursive(tree->root_layer());
2335 return layer ? layer->GetPicture() : skia::RefPtr<SkPicture>();
[email protected]b9dcf43a2013-01-09 00:15:292336}
2337
[email protected]c1bb5af2013-03-13 19:06:272338void LayerTreeHostImpl::SetDebugState(const LayerTreeDebugState& debug_state) {
[email protected]846f455b2013-03-18 19:07:412339 if (debug_state_.continuous_painting != debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272340 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242341
[email protected]c1bb5af2013-03-13 19:06:272342 debug_state_ = debug_state;
[email protected]d0d12192013-02-08 19:02:022343}
2344
[email protected]d3143c732012-10-05 19:17:592345} // namespace cc