blob: ffcb3b362c135c3d57e999dd8d74ac7f7e178ae5 [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]4456eee22012-10-19 18:16:3810#include "base/debug/trace_event.h"
[email protected]4a23c374c2012-12-08 08:38:5511#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1412#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3413#include "base/stl_util.h"
[email protected]131a0c22013-02-12 18:31:0814#include "base/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0915#include "cc/animation/scrollbar_animation_controller.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]3052b10f2013-03-18 07:41:2123#include "cc/input/page_scale_animation.h"
24#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5225#include "cc/layers/append_quads_data.h"
26#include "cc/layers/heads_up_display_layer_impl.h"
27#include "cc/layers/layer_iterator.h"
28#include "cc/layers/scrollbar_layer_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3029#include "cc/output/compositor_frame_metadata.h"
30#include "cc/output/delegating_renderer.h"
31#include "cc/output/gl_renderer.h"
32#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5633#include "cc/quads/render_pass_draw_quad.h"
34#include "cc/quads/shared_quad_state.h"
35#include "cc/quads/solid_color_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4036#include "cc/resources/memory_history.h"
37#include "cc/resources/picture_layer_tiling.h"
38#include "cc/resources/prioritized_resource_manager.h"
[email protected]be4655a2013-03-18 08:36:3139#include "cc/scheduler/delay_based_time_source.h"
40#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0441#include "cc/trees/damage_tracker.h"
42#include "cc/trees/layer_tree_host.h"
43#include "cc/trees/layer_tree_host_common.h"
44#include "cc/trees/layer_tree_impl.h"
45#include "cc/trees/quad_culler.h"
46#include "cc/trees/single_thread_proxy.h"
47#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0648#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1349#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1450
[email protected]94f206c12012-08-25 00:09:1451namespace {
52
[email protected]c1bb5af2013-03-13 19:06:2753void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
54 if (visible) {
55 TRACE_EVENT_ASYNC_BEGIN1("webkit",
56 "LayerTreeHostImpl::SetVisible",
57 id,
58 "LayerTreeHostImpl",
59 id);
60 return;
61 }
[email protected]94f206c12012-08-25 00:09:1462
[email protected]c1bb5af2013-03-13 19:06:2763 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1464}
65
[email protected]c1bb5af2013-03-13 19:06:2766std::string ValueToString(scoped_ptr<base::Value> value) {
67 std::string str;
68 base::JSONWriter::Write(value.get(), &str);
69 return str;
[email protected]131a0c22013-02-12 18:31:0870}
71
[email protected]c1bb5af2013-03-13 19:06:2772} // namespace
[email protected]94f206c12012-08-25 00:09:1473
[email protected]9c88e562012-09-14 22:21:3074namespace cc {
[email protected]94f206c12012-08-25 00:09:1475
[email protected]96baf3e2012-10-22 23:09:5576class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:2777 public:
78 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
79 LayerTreeHostImpl* layer_tree_host_impl,
80 scoped_refptr<DelayBasedTimeSource> time_source) {
81 return make_scoped_ptr(
82 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
83 time_source));
84 }
85 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:2686 time_source_->SetClient(NULL);
87 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:2788 }
89
[email protected]6d0e69d2013-03-20 14:53:2690 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:2791 // In single threaded mode we attempt to simulate changing the current
92 // thread by maintaining a fake thread id. When we switch from one
93 // thread to another, we construct DebugScopedSetXXXThread objects that
94 // update the thread id. This lets DCHECKS that ensure we're on the
95 // right thread to work correctly in single threaded mode. The problem
96 // here is that the timer tasks are run via the message loop, and when
97 // they run, we've had no chance to construct a DebugScopedSetXXXThread
98 // object. The result is that we report that we're running on the main
99 // thread. In multi-threaded mode, this timer is run on the compositor
100 // thread, so to keep this consistent in single-threaded mode, we'll
101 // construct a DebugScopedSetImplThread object. There is no need to do
102 // this in multi-threaded mode since the real thread id's will be
103 // correct. In fact, setting fake thread id's interferes with the real
104 // thread id's and causes breakage.
105 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
106 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
107 set_impl_thread.reset(
108 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14109 }
110
[email protected]c1bb5af2013-03-13 19:06:27111 layer_tree_host_impl_->ActivatePendingTreeIfNeeded();
112 layer_tree_host_impl_->Animate(base::TimeTicks::Now(), base::Time::Now());
113 layer_tree_host_impl_->BeginNextFrame();
114 }
[email protected]373974232013-01-10 22:20:50115
[email protected]c1bb5af2013-03-13 19:06:27116 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26117 if (active != time_source_->Active())
118 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27119 }
[email protected]94f206c12012-08-25 00:09:14120
[email protected]c1bb5af2013-03-13 19:06:27121 private:
122 LayerTreeHostImplTimeSourceAdapter(
123 LayerTreeHostImpl* layer_tree_host_impl,
124 scoped_refptr<DelayBasedTimeSource> time_source)
125 : layer_tree_host_impl_(layer_tree_host_impl),
126 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26127 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27128 }
[email protected]94f206c12012-08-25 00:09:14129
[email protected]c1bb5af2013-03-13 19:06:27130 LayerTreeHostImpl* layer_tree_host_impl_;
131 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14132
[email protected]c1bb5af2013-03-13 19:06:27133 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14134};
135
[email protected]96baf3e2012-10-22 23:09:55136LayerTreeHostImpl::FrameData::FrameData()
[email protected]c1bb5af2013-03-13 19:06:27137 : contains_incomplete_tile(false) {}
138
139LayerTreeHostImpl::FrameData::~FrameData() {}
140
141scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
142 const LayerTreeSettings& settings,
143 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43144 Proxy* proxy,
145 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
146 return make_scoped_ptr(
147 new LayerTreeHostImpl(settings,
148 client,
149 proxy,
150 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10151}
152
[email protected]372bad5f2013-03-21 16:38:43153LayerTreeHostImpl::LayerTreeHostImpl(
154 const LayerTreeSettings& settings,
155 LayerTreeHostImplClient* client,
156 Proxy* proxy,
157 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27158 : client_(client),
159 proxy_(proxy),
160 did_lock_scrolling_layer_(false),
161 should_bubble_scrolls_(false),
162 wheel_scrolling_(false),
163 settings_(settings),
[email protected]d9083762013-03-24 01:36:40164 overdraw_bottom_height_(0.f),
[email protected]c1bb5af2013-03-13 19:06:27165 device_scale_factor_(1.f),
166 visible_(true),
167 managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34168 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27169 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
170 0,
171 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING),
172 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36173 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29174 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27175 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02176 debug_rect_history_(DebugRectHistory::Create()),
[email protected]c1bb5af2013-03-13 19:06:27177 last_sent_memory_visible_bytes_(0),
178 last_sent_memory_visible_and_nearby_bytes_(0),
179 last_sent_memory_use_bytes_(0),
[email protected]372bad5f2013-03-21 16:38:43180 animation_registrar_(AnimationRegistrar::Create()),
181 rendering_stats_instrumentation_(rendering_stats_instrumentation) {
[email protected]c1bb5af2013-03-13 19:06:27182 DCHECK(proxy_->IsImplThread());
183 DidVisibilityChange(this, visible_);
184
[email protected]8e0176d2013-03-21 03:14:52185 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27186
[email protected]8e0176d2013-03-21 03:14:52187 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27188 top_controls_manager_ =
189 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52190 settings.top_controls_height,
191 settings.top_controls_show_threshold,
192 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27193 }
194
[email protected]8e0176d2013-03-21 03:14:52195 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27196
197 // LTHI always has an active tree.
198 active_tree_ = LayerTreeImpl::create(this);
[email protected]493067512012-09-19 23:34:10199}
200
[email protected]c1bb5af2013-03-13 19:06:27201LayerTreeHostImpl::~LayerTreeHostImpl() {
202 DCHECK(proxy_->IsImplThread());
203 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
204
205 if (active_tree_->root_layer()) {
206 ClearRenderSurfaces();
207 // The layer trees must be destroyed before the layer tree host. We've
208 // made a contract with our animation controllers that the registrar
209 // will outlive them, and we must make good.
210 recycle_tree_.reset();
211 pending_tree_.reset();
212 active_tree_.reset();
213 }
[email protected]94f206c12012-08-25 00:09:14214}
215
[email protected]c1bb5af2013-03-13 19:06:27216void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29217
[email protected]c1bb5af2013-03-13 19:06:27218void LayerTreeHostImpl::CommitComplete() {
219 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08220
[email protected]c1bb5af2013-03-13 19:06:27221 // Impl-side painting needs an update immediately post-commit to have the
222 // opportunity to create tilings. Other paths can call UpdateDrawProperties
223 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52224 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27225 pending_tree_->set_needs_update_draw_properties();
226 pending_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_PENDING_TREE);
227 } else {
228 active_tree_->set_needs_update_draw_properties();
229 }
[email protected]3ba4cae2013-01-16 03:58:38230
[email protected]c1bb5af2013-03-13 19:06:27231 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14232}
233
[email protected]c1bb5af2013-03-13 19:06:27234bool LayerTreeHostImpl::CanDraw() {
235 // Note: If you are changing this function or any other function that might
236 // affect the result of CanDraw, make sure to call
237 // client_->OnCanDrawStateChanged in the proper places and update the
238 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14239
[email protected]c1bb5af2013-03-13 19:06:27240 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42241 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
242 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22243 return false;
[email protected]c1bb5af2013-03-13 19:06:27244 }
245 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42246 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
247 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27248 return false;
249 }
250 if (active_tree_->ViewportSizeInvalid()) {
251 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42252 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
253 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27254 return false;
255 }
256 if (!renderer_) {
[email protected]c76faea2013-03-26 07:42:42257 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
258 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27259 return false;
260 }
261 if (active_tree_->ContentsTexturesPurged()) {
262 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42263 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
264 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27265 return false;
266 }
267 return true;
[email protected]2f1acc262012-11-16 21:42:22268}
269
[email protected]c1bb5af2013-03-13 19:06:27270void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
271 base::Time wall_clock_time) {
272 AnimatePageScale(monotonic_time);
273 AnimateLayers(monotonic_time, wall_clock_time);
274 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37275 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14276}
277
[email protected]c1bb5af2013-03-13 19:06:27278void LayerTreeHostImpl::ManageTiles() {
279 DCHECK(tile_manager_);
280 tile_manager_->ManageTiles();
281
282 size_t memory_required_bytes;
283 size_t memory_nice_to_have_bytes;
284 size_t memory_used_bytes;
285 tile_manager_->GetMemoryStats(&memory_required_bytes,
286 &memory_nice_to_have_bytes,
287 &memory_used_bytes);
288 SendManagedMemoryStats(memory_required_bytes,
289 memory_nice_to_have_bytes,
290 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15291}
292
[email protected]80334ba2013-03-20 17:33:50293void LayerTreeHostImpl::SetAnticipatedDrawTime(base::TimeTicks time) {
[email protected]f8456612013-03-19 20:56:09294 if (tile_manager_)
295 tile_manager_->SetAnticipatedDrawTime(time);
296}
297
[email protected]c1bb5af2013-03-13 19:06:27298void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
299 bool anchor_point,
300 float page_scale,
301 base::TimeTicks start_time,
302 base::TimeDelta duration) {
303 if (!RootScrollLayer())
304 return;
305
306 gfx::Vector2dF scroll_total =
307 RootScrollLayer()->scroll_offset() + RootScrollLayer()->scroll_delta();
308 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51309 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27310
311 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
312 page_scale_animation_ =
313 PageScaleAnimation::Create(scroll_total,
314 active_tree_->total_page_scale_factor(),
315 viewport_size,
316 scaled_scrollable_size,
317 start_time_seconds);
318
319 if (anchor_point) {
320 gfx::Vector2dF anchor(target_offset);
321 page_scale_animation_->ZoomWithAnchor(anchor,
322 page_scale,
323 duration.InSecondsF());
324 } else {
325 gfx::Vector2dF scaled_target_offset = target_offset;
326 page_scale_animation_->ZoomTo(scaled_target_offset,
327 page_scale,
328 duration.InSecondsF());
329 }
330
331 client_->SetNeedsRedrawOnImplThread();
332 client_->SetNeedsCommitOnImplThread();
333 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15334}
335
[email protected]c1bb5af2013-03-13 19:06:27336void LayerTreeHostImpl::ScheduleAnimation() {
337 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15338}
339
[email protected]c1bb5af2013-03-13 19:06:27340bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
341 if (!EnsureRenderSurfaceLayerList())
342 return false;
[email protected]f57bbc02012-11-21 07:02:15343
[email protected]c1bb5af2013-03-13 19:06:27344 gfx::PointF device_viewport_point =
345 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15346
[email protected]c1bb5af2013-03-13 19:06:27347 // First find out which layer was hit from the saved list of visible layers
348 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39349 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27350 device_viewport_point,
351 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15352
[email protected]c1bb5af2013-03-13 19:06:27353 // Walk up the hierarchy and look for a layer with a touch event handler
354 // region that the given point hits.
355 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39356 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27357 layer_impl))
358 return true;
359 }
[email protected]f57bbc02012-11-21 07:02:15360
[email protected]c1bb5af2013-03-13 19:06:27361 return false;
362}
363
364void LayerTreeHostImpl::TrackDamageForAllSurfaces(
365 LayerImpl* root_draw_layer,
366 const LayerList& render_surface_layer_list) {
367 // For now, we use damage tracking to compute a global scissor. To do this, we
368 // must compute all damage tracking before drawing anything, so that we know
369 // the root damage rect. The root damage rect is then used to scissor each
370 // surface.
371
372 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06373 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27374 --surface_index) {
375 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
376 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
377 DCHECK(render_surface);
378 render_surface->damage_tracker()->UpdateDamageTrackingState(
379 render_surface->layer_list(),
380 render_surface_layer->id(),
381 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
382 render_surface->content_rect(),
383 render_surface_layer->mask_layer(),
384 render_surface_layer->filters(),
385 render_surface_layer->filter().get());
386 }
387}
388
389void LayerTreeHostImpl::FrameData::AppendRenderPass(
390 scoped_ptr<RenderPass> render_pass) {
391 render_passes_by_id[render_pass->id] = render_pass.get();
392 render_passes.push_back(render_pass.Pass());
393}
394
395static void AppendQuadsForLayer(RenderPass* target_render_pass,
396 LayerImpl* layer,
397 const OcclusionTrackerImpl& occlusion_tracker,
398 AppendQuadsData* append_quads_data) {
399 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49400 QuadCuller quad_culler(&target_render_pass->quad_list,
401 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27402 layer,
403 occlusion_tracker,
404 layer->ShowDebugBorders(),
405 for_surface);
406 layer->AppendQuads(&quad_culler, append_quads_data);
407}
408
409static void AppendQuadsForRenderSurfaceLayer(
410 RenderPass* target_render_pass,
411 LayerImpl* layer,
412 const RenderPass* contributing_render_pass,
413 const OcclusionTrackerImpl& occlusion_tracker,
414 AppendQuadsData* append_quads_data) {
415 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49416 QuadCuller quad_culler(&target_render_pass->quad_list,
417 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27418 layer,
419 occlusion_tracker,
420 layer->ShowDebugBorders(),
421 for_surface);
422
423 bool is_replica = false;
424 layer->render_surface()->AppendQuads(&quad_culler,
425 append_quads_data,
426 is_replica,
427 contributing_render_pass->id);
428
429 // Add replica after the surface so that it appears below the surface.
430 if (layer->has_replica()) {
431 is_replica = true;
432 layer->render_surface()->AppendQuads(&quad_culler,
433 append_quads_data,
434 is_replica,
435 contributing_render_pass->id);
436 }
437}
438
439static void AppendQuadsToFillScreen(
440 RenderPass* target_render_pass,
441 LayerImpl* root_layer,
442 SkColor screen_background_color,
443 const OcclusionTrackerImpl& occlusion_tracker) {
444 if (!root_layer || !SkColorGetA(screen_background_color))
445 return;
446
447 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
448 if (fill_region.IsEmpty())
449 return;
450
451 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49452 QuadCuller quad_culler(&target_render_pass->quad_list,
453 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27454 root_layer,
455 occlusion_tracker,
456 root_layer->ShowDebugBorders(),
457 for_surface);
458
459 // Manually create the quad state for the gutter quads, as the root layer
460 // doesn't have any bounds and so can't generate this itself.
461 // TODO(danakj): Make the gutter quads generated by the solid color layer
462 // (make it smarter about generating quads to fill unoccluded areas).
463
464 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
465 float opacity = 1.f;
466 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49467 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27468 shared_quad_state->SetAll(root_layer->draw_transform(),
469 root_target_rect.size(),
470 root_target_rect,
471 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01472 false,
[email protected]f57bbc02012-11-21 07:02:15473 opacity);
474
[email protected]c1bb5af2013-03-13 19:06:27475 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17476
[email protected]c1bb5af2013-03-13 19:06:27477 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
478 bool did_invert = root_layer->screen_space_transform().GetInverse(
479 &transform_to_layer_space);
480 DCHECK(did_invert);
481 for (Region::Iterator fill_rects(fill_region);
482 fill_rects.has_rect();
483 fill_rects.next()) {
484 // The root layer transform is composed of translations and scales only,
485 // no perspective, so mapping is sufficient (as opposed to projecting).
486 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21487 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27488 // Skip the quad culler and just append the quads directly to avoid
489 // occlusion checks.
490 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
491 quad->SetNew(shared_quad_state, layer_rect, screen_background_color);
[email protected]c7e95b42013-03-18 01:13:49492 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27493 }
[email protected]467b3612012-08-28 07:41:16494}
495
[email protected]c1bb5af2013-03-13 19:06:27496bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
497 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14498
[email protected]c1bb5af2013-03-13 19:06:27499 if (!CanDraw() || !active_tree_->root_layer())
500 return false;
[email protected]2d692992012-12-19 01:19:32501
[email protected]c1bb5af2013-03-13 19:06:27502 TrackDamageForAllSurfaces(active_tree_->root_layer(),
503 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14504
[email protected]c1bb5af2013-03-13 19:06:27505 TRACE_EVENT1("cc",
506 "LayerTreeHostImpl::CalculateRenderPasses",
507 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06508 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14509
[email protected]c1bb5af2013-03-13 19:06:27510 // Create the render passes in dependency order.
511 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06512 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27513 --surface_index) {
514 LayerImpl* render_surface_layer =
515 (*frame->render_surface_layer_list)[surface_index];
516 render_surface_layer->render_surface()->AppendRenderPasses(frame);
517 }
[email protected]94f206c12012-08-25 00:09:14518
[email protected]c1bb5af2013-03-13 19:06:27519 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52520 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27521 base::debug::TraceLog::GetInstance() &&
522 base::debug::TraceLog::GetInstance()->IsEnabled();
523 OcclusionTrackerImpl occlusion_tracker(
524 active_tree_->root_layer()->render_surface()->content_rect(),
525 record_metrics_for_frame);
526 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52527 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14528
[email protected]846f455b2013-03-18 19:07:41529 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27530 occlusion_tracker.set_occluding_screen_space_rects_container(
531 &frame->occluding_screen_space_rects);
532 }
[email protected]846f455b2013-03-18 19:07:41533 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27534 occlusion_tracker.set_non_occluding_screen_space_rects_container(
535 &frame->non_occluding_screen_space_rects);
536 }
[email protected]94f206c12012-08-25 00:09:14537
[email protected]c1bb5af2013-03-13 19:06:27538 // Add quads to the Render passes in FrontToBack order to allow for testing
539 // occlusion and performing culling during the tree walk.
540 typedef LayerIterator<LayerImpl,
541 std::vector<LayerImpl*>,
542 RenderSurfaceImpl,
543 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14544
[email protected]c1bb5af2013-03-13 19:06:27545 // Typically when we are missing a texture and use a checkerboard quad, we
546 // still draw the frame. However when the layer being checkerboarded is moving
547 // due to an impl-animation, we drop the frame to avoid flashing due to the
548 // texture suddenly appearing in the future.
549 bool draw_frame = true;
[email protected]94f206c12012-08-25 00:09:14550
[email protected]372bad5f2013-03-21 16:38:43551 int layers_drawn = 0;
552
[email protected]c1bb5af2013-03-13 19:06:27553 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09554 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27555 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09556 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27557 it != end;
558 ++it) {
559 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09560 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27561 RenderPass* target_render_pass =
562 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14563
[email protected]c1bb5af2013-03-13 19:06:27564 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14565
[email protected]c1bb5af2013-03-13 19:06:27566 AppendQuadsData append_quads_data(target_render_pass->id);
[email protected]89228202012-08-29 03:20:30567
[email protected]71dfcc72013-03-20 21:30:09568 if (it.represents_contributing_render_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27569 RenderPass::Id contributing_render_pass_id =
570 it->render_surface()->RenderPassId();
571 RenderPass* contributing_render_pass =
572 frame->render_passes_by_id[contributing_render_pass_id];
573 AppendQuadsForRenderSurfaceLayer(target_render_pass,
574 *it,
575 contributing_render_pass,
576 occlusion_tracker,
577 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09578 } else if (it.represents_itself() &&
579 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27580 bool has_occlusion_from_outside_target_surface;
581 bool impl_draw_transform_is_unknown = false;
582 if (occlusion_tracker.Occluded(
583 it->render_target(),
584 it->visible_content_rect(),
585 it->draw_transform(),
586 impl_draw_transform_is_unknown,
587 it->is_clipped(),
588 it->clip_rect(),
589 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09590 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27591 has_occlusion_from_outside_target_surface;
592 } else {
593 DCHECK_EQ(active_tree_, it->layer_tree_impl());
594 it->WillDraw(resource_provider_.get());
595 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57596
[email protected]c1bb5af2013-03-13 19:06:27597 if (it->HasContributingDelegatedRenderPasses()) {
598 RenderPass::Id contributing_render_pass_id =
599 it->FirstContributingRenderPassId();
600 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
601 frame->render_passes_by_id.end()) {
602 RenderPass* render_pass =
603 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14604
[email protected]c1bb5af2013-03-13 19:06:27605 AppendQuadsData append_quads_data(render_pass->id);
606 AppendQuadsForLayer(render_pass,
607 *it,
608 occlusion_tracker,
609 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57610
[email protected]c1bb5af2013-03-13 19:06:27611 contributing_render_pass_id =
612 it->NextContributingRenderPassId(contributing_render_pass_id);
613 }
[email protected]94f206c12012-08-25 00:09:14614 }
615
[email protected]c1bb5af2013-03-13 19:06:27616 AppendQuadsForLayer(target_render_pass,
617 *it,
618 occlusion_tracker,
619 &append_quads_data);
620 }
[email protected]89228202012-08-29 03:20:30621
[email protected]372bad5f2013-03-21 16:38:43622 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14623 }
624
[email protected]e1e768f2013-03-26 08:48:09625 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27626 target_render_pass->has_occlusion_from_outside_target_surface = true;
627
[email protected]e1e768f2013-03-26 08:48:09628 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43629 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09630 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27631 bool layer_has_animating_transform =
632 it->screen_space_transform_is_animating() ||
633 it->draw_transform_is_animating();
634 if (layer_has_animating_transform)
635 draw_frame = false;
636 }
637
[email protected]e1e768f2013-03-26 08:48:09638 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27639 frame->contains_incomplete_tile = true;
640
641 occlusion_tracker.LeaveLayer(it);
642 }
643
[email protected]372bad5f2013-03-21 16:38:43644 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
645
[email protected]1d993172012-10-18 18:15:04646#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27647 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
648 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
649 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
650 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
651 != frame->render_passes_by_id.end());
652 }
[email protected]94f206c12012-08-25 00:09:14653#endif
[email protected]c1bb5af2013-03-13 19:06:27654 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14655
[email protected]c1bb5af2013-03-13 19:06:27656 if (!active_tree_->has_transparent_background()) {
657 frame->render_passes.back()->has_transparent_background = false;
658 AppendQuadsToFillScreen(frame->render_passes.back(),
659 active_tree_->root_layer(),
660 active_tree_->background_color(),
661 occlusion_tracker);
662 }
[email protected]94f206c12012-08-25 00:09:14663
[email protected]c1bb5af2013-03-13 19:06:27664 if (draw_frame)
665 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]94f206c12012-08-25 00:09:14666
[email protected]c1bb5af2013-03-13 19:06:27667 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
668 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]bf691c22013-03-26 21:15:06669 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
670 frame);
[email protected]94f206c12012-08-25 00:09:14671
[email protected]c1bb5af2013-03-13 19:06:27672 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14673}
674
[email protected]c1bb5af2013-03-13 19:06:27675void LayerTreeHostImpl::SetBackgroundTickingEnabled(bool enabled) {
676 // Lazily create the time_source adapter so that we can vary the interval for
677 // testing.
678 if (!time_source_client_adapter_) {
679 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
680 this,
[email protected]6d0e69d2013-03-20 14:53:26681 DelayBasedTimeSource::Create(LowFrequencyAnimationInterval(),
[email protected]c1bb5af2013-03-13 19:06:27682 proxy_->CurrentThread()));
683 }
[email protected]94f206c12012-08-25 00:09:14684
[email protected]c1bb5af2013-03-13 19:06:27685 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14686}
687
[email protected]c1bb5af2013-03-13 19:06:27688static inline RenderPass* FindRenderPassById(
689 RenderPass::Id render_pass_id,
690 const LayerTreeHostImpl::FrameData& frame) {
691 RenderPassIdHashMap::const_iterator it =
692 frame.render_passes_by_id.find(render_pass_id);
693 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14694}
695
[email protected]c1bb5af2013-03-13 19:06:27696static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
697 LayerTreeHostImpl::FrameData* frame) {
698 RenderPass* remove_render_pass =
699 FindRenderPassById(remove_render_pass_id, *frame);
700 // The pass was already removed by another quad - probably the original, and
701 // we are the replica.
702 if (!remove_render_pass)
703 return;
704 RenderPassList& render_passes = frame->render_passes;
705 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
706 render_passes.end(),
707 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14708
[email protected]c1bb5af2013-03-13 19:06:27709 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14710
[email protected]c1bb5af2013-03-13 19:06:27711 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
712 frame->render_passes.erase(to_remove);
713 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14714
[email protected]c1bb5af2013-03-13 19:06:27715 // Now follow up for all RenderPass quads and remove their RenderPasses
716 // recursively.
717 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29718 QuadList::ConstBackToFrontIterator quad_list_iterator =
719 quad_list.BackToFrontBegin();
720 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27721 ++quad_list_iterator) {
722 DrawQuad* current_quad = (*quad_list_iterator);
723 if (current_quad->material != DrawQuad::RENDER_PASS)
724 continue;
[email protected]94f206c12012-08-25 00:09:14725
[email protected]c1bb5af2013-03-13 19:06:27726 RenderPass::Id next_remove_render_pass_id =
727 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
728 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
729 }
[email protected]94f206c12012-08-25 00:09:14730}
731
[email protected]c1bb5af2013-03-13 19:06:27732bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
733 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
734 const FrameData& frame) const {
735 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
736 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06737 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27738 if (quad_has_damage) {
739 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
740 return false;
741 } else if (!quad_has_cached_resource) {
742 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
743 return false;
744 }
745 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
746 return true;
[email protected]94f206c12012-08-25 00:09:14747}
748
[email protected]c1bb5af2013-03-13 19:06:27749bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
750 const RenderPassDrawQuad& quad, const FrameData& frame) const {
751 const RenderPass* render_pass =
752 FindRenderPassById(quad.render_pass_id, frame);
753 if (!render_pass)
754 return false;
[email protected]94f206c12012-08-25 00:09:14755
[email protected]c1bb5af2013-03-13 19:06:27756 // If any quad or RenderPass draws into this RenderPass, then keep it.
757 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29758 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
759 quad_list.BackToFrontBegin();
760 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27761 ++quad_list_iterator) {
762 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14763
[email protected]c1bb5af2013-03-13 19:06:27764 if (current_quad->material != DrawQuad::RENDER_PASS)
765 return false;
[email protected]94f206c12012-08-25 00:09:14766
[email protected]c1bb5af2013-03-13 19:06:27767 const RenderPass* contributing_pass = FindRenderPassById(
768 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
769 if (contributing_pass)
770 return false;
771 }
772 return true;
[email protected]94f206c12012-08-25 00:09:14773}
774
775// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27776template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
777 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
778 CullRenderPassesWithCachedTextures culler, FrameData* frame);
779template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
780 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
781 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14782
783// static
[email protected]c1bb5af2013-03-13 19:06:27784template <typename RenderPassCuller>
785void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
786 FrameData* frame) {
787 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
788 it != culler.RenderPassListEnd(frame->render_passes);
789 it = culler.RenderPassListNext(it)) {
790 const RenderPass* current_pass = frame->render_passes[it];
791 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29792 QuadList::ConstBackToFrontIterator quad_list_iterator =
793 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14794
[email protected]ed511b8d2013-03-25 03:29:29795 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27796 ++quad_list_iterator) {
797 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14798
[email protected]c1bb5af2013-03-13 19:06:27799 if (current_quad->material != DrawQuad::RENDER_PASS)
800 continue;
[email protected]94f206c12012-08-25 00:09:14801
[email protected]c1bb5af2013-03-13 19:06:27802 const RenderPassDrawQuad* render_pass_quad =
803 RenderPassDrawQuad::MaterialCast(current_quad);
804 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
805 continue;
[email protected]94f206c12012-08-25 00:09:14806
[email protected]c1bb5af2013-03-13 19:06:27807 // We are changing the vector in the middle of iteration. Because we
808 // delete render passes that draw into the current pass, we are
809 // guaranteed that any data from the iterator to the end will not
810 // change. So, capture the iterator position from the end of the
811 // list, and restore it after the change.
812 size_t position_from_end = frame->render_passes.size() - it;
813 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
814 it = frame->render_passes.size() - position_from_end;
815 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14816 }
[email protected]c1bb5af2013-03-13 19:06:27817 }
[email protected]94f206c12012-08-25 00:09:14818}
819
[email protected]c1bb5af2013-03-13 19:06:27820bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame) {
821 TRACE_EVENT0("cc", "LayerTreeHostImpl::PrepareToDraw");
[email protected]94f206c12012-08-25 00:09:14822
[email protected]c1bb5af2013-03-13 19:06:27823 active_tree_->UpdateDrawProperties(
824 LayerTreeImpl::UPDATE_ACTIVE_TREE_FOR_DRAW);
[email protected]2e7ca422012-12-20 02:57:27825
[email protected]c1bb5af2013-03-13 19:06:27826 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
827 frame->render_passes.clear();
828 frame->render_passes_by_id.clear();
829 frame->will_draw_layers.clear();
[email protected]94f206c12012-08-25 00:09:14830
[email protected]c1bb5af2013-03-13 19:06:27831 if (!CalculateRenderPasses(frame))
832 return false;
[email protected]94f206c12012-08-25 00:09:14833
[email protected]c1bb5af2013-03-13 19:06:27834 // If we return true, then we expect DrawLayers() to be called before this
835 // function is called again.
836 return true;
[email protected]94f206c12012-08-25 00:09:14837}
838
[email protected]c1bb5af2013-03-13 19:06:27839void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
840 const ManagedMemoryPolicy& policy) {
841 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:35842 visible_ ? policy.bytes_limit_when_visible
843 : policy.bytes_limit_when_not_visible,
844 ManagedMemoryPolicy::PriorityCutoffToValue(
845 visible_ ? policy.priority_cutoff_when_visible
846 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:27847 if (evicted_resources) {
848 active_tree_->SetContentsTexturesPurged();
849 if (pending_tree_)
850 pending_tree_->SetContentsTexturesPurged();
851 client_->SetNeedsCommitOnImplThread();
852 client_->OnCanDrawStateChanged(CanDraw());
853 client_->RenewTreePriority();
854 }
855 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:43856
[email protected]c1bb5af2013-03-13 19:06:27857 if (tile_manager_) {
858 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
859 new_state.memory_limit_in_bytes = visible_ ?
[email protected]46b8acc2013-03-19 22:38:35860 policy.bytes_limit_when_visible :
861 policy.bytes_limit_when_not_visible;
[email protected]c1bb5af2013-03-13 19:06:27862 new_state.memory_limit_policy =
[email protected]46b8acc2013-03-19 22:38:35863 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
[email protected]c1bb5af2013-03-13 19:06:27864 visible_ ?
[email protected]46b8acc2013-03-19 22:38:35865 policy.priority_cutoff_when_visible :
866 policy.priority_cutoff_when_not_visible);
[email protected]c1bb5af2013-03-13 19:06:27867 tile_manager_->SetGlobalState(new_state);
868 }
[email protected]94f206c12012-08-25 00:09:14869}
870
[email protected]c1bb5af2013-03-13 19:06:27871bool LayerTreeHostImpl::HasImplThread() const {
872 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:44873}
874
[email protected]c1bb5af2013-03-13 19:06:27875void LayerTreeHostImpl::ScheduleManageTiles() {
876 if (client_)
877 client_->SetNeedsManageTilesOnImplThread();
[email protected]8947cbe2012-11-28 05:27:43878}
879
[email protected]86126792013-03-16 20:07:54880void LayerTreeHostImpl::DidInitializeVisibleTile() {
881 // TODO(reveman): Determine tiles that changed and only damage
882 // what's necessary.
883 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:27884 if (client_)
[email protected]86126792013-03-16 20:07:54885 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:47886}
887
[email protected]c1bb5af2013-03-13 19:06:27888bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:52889 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:52890}
891
[email protected]c1bb5af2013-03-13 19:06:27892void LayerTreeHostImpl::SetManagedMemoryPolicy(
893 const ManagedMemoryPolicy& policy) {
894 if (managed_memory_policy_ == policy)
895 return;
[email protected]61de5812012-11-08 07:03:44896
[email protected]c1bb5af2013-03-13 19:06:27897 managed_memory_policy_ = policy;
898 if (!proxy_->HasImplThread()) {
899 // TODO(ccameron): In single-thread mode, this can be called on the main
900 // thread by GLRenderer::OnMemoryAllocationChanged.
901 DebugScopedSetImplThread impl_thread(proxy_);
902 EnforceManagedMemoryPolicy(managed_memory_policy_);
903 } else {
904 DCHECK(proxy_->IsImplThread());
905 EnforceManagedMemoryPolicy(managed_memory_policy_);
906 }
907 // We always need to commit after changing the memory policy because the new
908 // limit can result in more or less content having texture allocated for it.
909 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:14910}
911
[email protected]c1bb5af2013-03-13 19:06:27912void LayerTreeHostImpl::OnVSyncParametersChanged(base::TimeTicks timebase,
913 base::TimeDelta interval) {
914 client_->OnVSyncParametersChanged(timebase, interval);
[email protected]94f206c12012-08-25 00:09:14915}
916
[email protected]7ed47512013-03-26 22:28:45917void LayerTreeHostImpl::DidVSync(base::TimeTicks frame_time) {
918 client_->DidVSync(frame_time);
919}
920
[email protected]c1bb5af2013-03-13 19:06:27921void LayerTreeHostImpl::OnSendFrameToParentCompositorAck(
922 const CompositorFrameAck& ack) {
923 if (!renderer_)
924 return;
[email protected]b6f3d7e2012-12-08 00:11:21925
[email protected]c1bb5af2013-03-13 19:06:27926 // TODO(piman): We may need to do some validation on this ack before
927 // processing it.
928 renderer_->ReceiveCompositorFrameAck(ack);
[email protected]a46f32932012-12-07 21:43:16929}
930
[email protected]c1bb5af2013-03-13 19:06:27931void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
932 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:29933}
934
[email protected]c1bb5af2013-03-13 19:06:27935CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
936 CompositorFrameMetadata metadata;
937 metadata.device_scale_factor = device_scale_factor_;
938 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
939 metadata.viewport_size = active_tree_->ScrollableViewportSize();
940 metadata.root_layer_size = active_tree_->ScrollableSize();
941 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
942 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
943 if (top_controls_manager_) {
944 metadata.location_bar_offset =
945 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
946 metadata.location_bar_content_translation =
947 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:01948 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:27949 }
[email protected]bf189f62012-12-18 03:42:11950
[email protected]c1bb5af2013-03-13 19:06:27951 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:11952 return metadata;
[email protected]c1bb5af2013-03-13 19:06:27953
[email protected]ffb2720f2013-03-15 19:18:37954 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27955
956 return metadata;
[email protected]bf189f62012-12-18 03:42:11957}
958
[email protected]f0c2a242013-03-15 19:34:52959void LayerTreeHostImpl::DrawLayers(FrameData* frame,
960 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:27961 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
962 DCHECK(CanDraw());
963 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14964
[email protected]9e3594522013-03-18 00:57:36965 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:14966
[email protected]372bad5f2013-03-21 16:38:43967 rendering_stats_instrumentation_->SetScreenFrameCount(
968 fps_counter_->current_frame_number());
969 rendering_stats_instrumentation_->SetDroppedFrameCount(
970 fps_counter_->dropped_frame_count());
971
[email protected]c1bb5af2013-03-13 19:06:27972 if (tile_manager_) {
973 memory_history_->SaveEntry(
974 tile_manager_->memory_stats_from_last_assign());
975 }
[email protected]1191d9d2013-02-02 06:00:33976
[email protected]846f455b2013-03-18 19:07:41977 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:02978 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:27979 active_tree_->root_layer(),
980 *frame->render_surface_layer_list,
981 frame->occluding_screen_space_rects,
982 frame->non_occluding_screen_space_rects,
983 debug_state_);
984 }
[email protected]94f206c12012-08-25 00:09:14985
[email protected]a848c102013-03-26 08:59:09986 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
987 const RenderingStats& stats =
988 rendering_stats_instrumentation_->GetRenderingStats();
989 paint_time_counter_->SavePaintTime(stats.total_paint_time);
990 }
991
[email protected]846f455b2013-03-18 19:07:41992 if (debug_state_.trace_all_rendered_frames) {
[email protected]c76faea2013-03-26 07:42:42993 TRACE_EVENT_INSTANT1("cc.debug", "Frame", TRACE_EVENT_SCOPE_THREAD,
[email protected]c1bb5af2013-03-13 19:06:27994 "frame", ValueToString(FrameStateAsValue()));
995 }
[email protected]131a0c22013-02-12 18:31:08996
[email protected]c1bb5af2013-03-13 19:06:27997 // Because the contents of the HUD depend on everything else in the frame, the
998 // contents of its texture are updated as the last thing before the frame is
999 // drawn.
1000 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541001 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141002
[email protected]d002dd02013-03-27 07:40:401003 renderer_->DrawFrame(&frame->render_passes);
[email protected]c1bb5af2013-03-13 19:06:271004 // The render passes should be consumed by the renderer.
1005 DCHECK(frame->render_passes.empty());
1006 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141007
[email protected]c1bb5af2013-03-13 19:06:271008 // The next frame should start by assuming nothing has changed, and changes
1009 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541010 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271011 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1012 DidDrawDamagedArea();
1013 }
1014 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
1015 UpdateAnimationState();
[email protected]94f206c12012-08-25 00:09:141016}
1017
[email protected]c1bb5af2013-03-13 19:06:271018void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1019 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1020 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521021
[email protected]c1bb5af2013-03-13 19:06:271022 // Once all layers have been drawn, pending texture uploads should no
1023 // longer block future uploads.
1024 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141025}
1026
[email protected]c1bb5af2013-03-13 19:06:271027void LayerTreeHostImpl::FinishAllRendering() {
1028 if (renderer_)
1029 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141030}
1031
[email protected]c1bb5af2013-03-13 19:06:271032bool LayerTreeHostImpl::IsContextLost() {
1033 DCHECK(proxy_->IsImplThread());
1034 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141035}
1036
[email protected]c1bb5af2013-03-13 19:06:271037const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1038 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141039}
1040
[email protected]c1bb5af2013-03-13 19:06:271041bool LayerTreeHostImpl::SwapBuffers() {
[email protected]c1bb5af2013-03-13 19:06:271042 return renderer_->SwapBuffers();
[email protected]94f206c12012-08-25 00:09:141043}
1044
[email protected]7ed47512013-03-26 22:28:451045void LayerTreeHostImpl::EnableVSyncNotification(bool enable) {
1046 if (output_surface_)
1047 output_surface_->EnableVSyncNotification(enable);
1048}
1049
[email protected]c1bb5af2013-03-13 19:06:271050gfx::Size LayerTreeHostImpl::DeviceViewportSize() const {
1051 return device_viewport_size();
[email protected]493067512012-09-19 23:34:101052}
1053
[email protected]ffb2720f2013-03-15 19:18:371054gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
1055 gfx::SizeF dip_size =
1056 gfx::ScaleSize(DeviceViewportSize(), 1.f / device_scale_factor());
1057
1058 // The clip layer should be used if non-overlay scrollbars may exist since
1059 // it adjusts for them.
1060 LayerImpl* clip_layer = active_tree_->RootClipLayer();
[email protected]8e0176d2013-03-21 03:14:521061 if (!Settings().solid_color_scrollbars && clip_layer &&
[email protected]ffb2720f2013-03-15 19:18:371062 clip_layer->masks_to_bounds())
1063 dip_size = clip_layer->bounds();
1064
[email protected]ed511b8d2013-03-25 03:29:291065 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371066 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401067 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291068 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371069}
1070
[email protected]c1bb5af2013-03-13 19:06:271071const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1072 return settings();
[email protected]493067512012-09-19 23:34:101073}
1074
[email protected]c1bb5af2013-03-13 19:06:271075void LayerTreeHostImpl::DidLoseOutputSurface() {
1076 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141077}
1078
[email protected]c1bb5af2013-03-13 19:06:271079void LayerTreeHostImpl::OnSwapBuffersComplete() {
1080 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]94f206c12012-08-25 00:09:141081}
1082
[email protected]c1bb5af2013-03-13 19:06:271083void LayerTreeHostImpl::Readback(void* pixels,
1084 gfx::Rect rect_in_device_viewport) {
1085 DCHECK(renderer_);
1086 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141087}
1088
[email protected]69b50ec2013-01-19 04:58:011089bool LayerTreeHostImpl::haveRootScrollLayer() const {
[email protected]c1bb5af2013-03-13 19:06:271090 return RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011091}
1092
[email protected]c1bb5af2013-03-13 19:06:271093LayerImpl* LayerTreeHostImpl::RootLayer() const {
1094 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081095}
1096
[email protected]c1bb5af2013-03-13 19:06:271097LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1098 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081099}
1100
[email protected]c1bb5af2013-03-13 19:06:271101LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1102 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081103}
1104
[email protected]94f206c12012-08-25 00:09:141105// Content layers can be either directly scrollable or contained in an outer
1106// scrolling layer which applies the scroll transform. Given a content layer,
1107// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271108static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1109 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141110 return 0;
[email protected]c1bb5af2013-03-13 19:06:271111
1112 if (layer_impl->scrollable())
1113 return layer_impl;
1114
1115 if (layer_impl->DrawsContent() &&
1116 layer_impl->parent() &&
1117 layer_impl->parent()->scrollable())
1118 return layer_impl->parent();
1119
1120 return 0;
[email protected]94f206c12012-08-25 00:09:141121}
1122
[email protected]c1bb5af2013-03-13 19:06:271123void LayerTreeHostImpl::CreatePendingTree() {
1124 CHECK(!pending_tree_);
1125 if (recycle_tree_)
1126 recycle_tree_.swap(pending_tree_);
1127 else
1128 pending_tree_ = LayerTreeImpl::create(this);
1129 client_->OnCanDrawStateChanged(CanDraw());
1130 client_->OnHasPendingTreeStateChanged(pending_tree_);
1131 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1132 TRACE_EVENT_ASYNC_STEP0("cc",
1133 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271134}
1135
[email protected]c1bb5af2013-03-13 19:06:271136void LayerTreeHostImpl::CheckForCompletedTileUploads() {
1137 DCHECK(!client_->IsInsideDraw()) <<
1138 "Checking for completed uploads within a draw may trigger "
1139 "spurious redraws.";
1140 if (tile_manager_)
1141 tile_manager_->CheckForCompletedTileUploads();
[email protected]eabe5002013-01-12 22:07:481142}
1143
[email protected]c1bb5af2013-03-13 19:06:271144bool LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1145 if (!pending_tree_)
1146 return false;
[email protected]2e7ca422012-12-20 02:57:271147
[email protected]c1bb5af2013-03-13 19:06:271148 CHECK(tile_manager_);
[email protected]2ae038b2013-01-28 12:52:091149
[email protected]c1bb5af2013-03-13 19:06:271150 pending_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_PENDING_TREE);
[email protected]615c78a2013-01-24 23:44:161151
[email protected]c1bb5af2013-03-13 19:06:271152 TRACE_EVENT_ASYNC_STEP1("cc",
1153 "PendingTree", pending_tree_.get(), "activate",
1154 "state", ValueToString(ActivationStateAsValue()));
[email protected]4f0a5002013-01-28 13:02:271155
[email protected]ed25b442013-03-26 21:52:401156 // Activate once all visible resources in pending tree are ready
[email protected]c1bb5af2013-03-13 19:06:271157 // or tile manager has no work scheduled for pending tree.
[email protected]ed25b442013-03-26 21:52:401158 if (!pending_tree_->AreVisibleResourcesReady()) {
[email protected]c1bb5af2013-03-13 19:06:271159 // In smoothness takes priority mode, the pending tree's priorities are
1160 // ignored, so the tile manager may not have work for it even though it
1161 // is simultaneously not ready to be activated.
1162 if (tile_manager_->GlobalState().tree_priority ==
1163 SMOOTHNESS_TAKES_PRIORITY ||
1164 tile_manager_->HasPendingWorkScheduled(PENDING_TREE)) {
1165 TRACE_EVENT_ASYNC_STEP0("cc",
1166 "PendingTree",
1167 pending_tree_.get(),
1168 "waiting");
1169 return false;
[email protected]131a0c22013-02-12 18:31:081170 }
[email protected]c1bb5af2013-03-13 19:06:271171 }
[email protected]2e7ca422012-12-20 02:57:271172
[email protected]c1bb5af2013-03-13 19:06:271173 ActivatePendingTree();
1174 return true;
[email protected]2e7ca422012-12-20 02:57:271175}
1176
[email protected]c1bb5af2013-03-13 19:06:271177void LayerTreeHostImpl::ActivatePendingTree() {
1178 CHECK(pending_tree_);
1179 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351180
[email protected]c1bb5af2013-03-13 19:06:271181 active_tree_->PushPersistedState(pending_tree_.get());
1182 if (pending_tree_->needs_full_tree_sync()) {
1183 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331184 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271185 active_tree_->DetachLayerTree(),
1186 active_tree_.get()));
1187 }
[email protected]b5651c22013-03-14 15:06:331188 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271189 active_tree_->root_layer());
1190 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511191
[email protected]c1bb5af2013-03-13 19:06:271192 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511193
[email protected]c1bb5af2013-03-13 19:06:271194 // Now that we've synced everything from the pending tree to the active
1195 // tree, rename the pending tree the recycle tree so we can reuse it on the
1196 // next sync.
1197 pending_tree_.swap(recycle_tree_);
1198 recycle_tree_->ClearRenderSurfaces();
[email protected]48871fc2013-01-23 07:36:511199
[email protected]c1bb5af2013-03-13 19:06:271200 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221201
[email protected]c1bb5af2013-03-13 19:06:271202 // Reduce wasted memory now that unlinked resources are guaranteed not
1203 // to be used.
1204 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411205
[email protected]c1bb5af2013-03-13 19:06:271206 client_->OnCanDrawStateChanged(CanDraw());
1207 client_->OnHasPendingTreeStateChanged(pending_tree_);
1208 client_->SetNeedsRedrawOnImplThread();
1209 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241210
[email protected]a848c102013-03-26 08:59:091211 if (debug_state_.continuous_painting) {
1212 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431213 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091214 paint_time_counter_->SavePaintTime(
1215 stats.total_paint_time +
1216 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271217 }
[email protected]2e7ca422012-12-20 02:57:271218}
1219
[email protected]c1bb5af2013-03-13 19:06:271220void LayerTreeHostImpl::SetVisible(bool visible) {
1221 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141222
[email protected]c1bb5af2013-03-13 19:06:271223 if (visible_ == visible)
1224 return;
1225 visible_ = visible;
1226 DidVisibilityChange(this, visible_);
1227 EnforceManagedMemoryPolicy(managed_memory_policy_);
[email protected]94f206c12012-08-25 00:09:141228
[email protected]c1bb5af2013-03-13 19:06:271229 if (!renderer_)
1230 return;
[email protected]94f206c12012-08-25 00:09:141231
[email protected]c1bb5af2013-03-13 19:06:271232 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141233
[email protected]c1bb5af2013-03-13 19:06:271234 SetBackgroundTickingEnabled(
1235 !visible_ &&
1236 !animation_registrar_->active_animation_controllers().empty());
[email protected]94f206c12012-08-25 00:09:141237}
1238
[email protected]c1bb5af2013-03-13 19:06:271239bool LayerTreeHostImpl::InitializeRenderer(
1240 scoped_ptr<OutputSurface> output_surface) {
1241 // Since we will create a new resource provider, we cannot continue to use
1242 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1243 // before we destroy the old resource provider.
1244 if (active_tree_->root_layer())
1245 ClearRenderSurfaces();
1246 if (active_tree_->root_layer())
1247 SendDidLoseOutputSurfaceRecursive(active_tree_->root_layer());
1248 if (pending_tree_ && pending_tree_->root_layer())
1249 SendDidLoseOutputSurfaceRecursive(pending_tree_->root_layer());
1250 if (recycle_tree_ && recycle_tree_->root_layer())
1251 SendDidLoseOutputSurfaceRecursive(recycle_tree_->root_layer());
[email protected]45c4b1e2013-01-16 02:19:401252
[email protected]c1bb5af2013-03-13 19:06:271253 // Note: order is important here.
1254 renderer_.reset();
1255 tile_manager_.reset();
1256 resource_provider_.reset();
1257 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141258
[email protected]c1bb5af2013-03-13 19:06:271259 if (!output_surface->BindToClient(this))
1260 return false;
[email protected]be3181652012-09-25 13:02:131261
[email protected]c1bb5af2013-03-13 19:06:271262 scoped_ptr<ResourceProvider> resource_provider =
1263 ResourceProvider::Create(output_surface.get());
1264 if (!resource_provider)
1265 return false;
[email protected]be3181652012-09-25 13:02:131266
[email protected]8e0176d2013-03-21 03:14:521267 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:271268 tile_manager_.reset(new TileManager(this,
1269 resource_provider.get(),
[email protected]8e0176d2013-03-21 03:14:521270 settings_.num_raster_threads,
1271 settings_.use_cheapness_estimator,
1272 settings_.use_color_estimator,
[email protected]372bad5f2013-03-21 16:38:431273 settings_.prediction_benchmarking,
1274 rendering_stats_instrumentation_));
[email protected]c1bb5af2013-03-13 19:06:271275 }
[email protected]8947cbe2012-11-28 05:27:431276
[email protected]c1bb5af2013-03-13 19:06:271277 if (output_surface->capabilities().has_parent_compositor) {
1278 renderer_ = DelegatingRenderer::Create(this, output_surface.get(),
1279 resource_provider.get());
1280 } else if (output_surface->context3d()) {
1281 renderer_ = GLRenderer::Create(this,
1282 output_surface.get(),
1283 resource_provider.get());
1284 } else if (output_surface->software_device()) {
1285 renderer_ = SoftwareRenderer::Create(this,
1286 output_surface.get(),
1287 resource_provider.get());
1288 }
1289 if (!renderer_)
1290 return false;
[email protected]be3181652012-09-25 13:02:131291
[email protected]c1bb5af2013-03-13 19:06:271292 resource_provider_ = resource_provider.Pass();
1293 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141294
[email protected]c1bb5af2013-03-13 19:06:271295 if (!visible_)
1296 renderer_->SetVisible(visible_);
[email protected]94f206c12012-08-25 00:09:141297
[email protected]c1bb5af2013-03-13 19:06:271298 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211299
[email protected]c1bb5af2013-03-13 19:06:271300 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1301 // to be initialized to get max texture size.
1302 active_tree_->set_needs_update_draw_properties();
1303 if (pending_tree_)
1304 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161305
[email protected]c1bb5af2013-03-13 19:06:271306 return true;
[email protected]94f206c12012-08-25 00:09:141307}
1308
[email protected]c1bb5af2013-03-13 19:06:271309void LayerTreeHostImpl::SetViewportSize(gfx::Size layout_viewport_size,
1310 gfx::Size device_viewport_size) {
1311 if (layout_viewport_size == layout_viewport_size_ &&
1312 device_viewport_size == device_viewport_size_)
1313 return;
[email protected]94f206c12012-08-25 00:09:141314
[email protected]c1bb5af2013-03-13 19:06:271315 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1316 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271317
[email protected]c1bb5af2013-03-13 19:06:271318 layout_viewport_size_ = layout_viewport_size;
1319 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141320
[email protected]c1bb5af2013-03-13 19:06:271321 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141322
[email protected]c1bb5af2013-03-13 19:06:271323 if (renderer_)
1324 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211325
[email protected]c1bb5af2013-03-13 19:06:271326 client_->OnCanDrawStateChanged(CanDraw());
[email protected]94f206c12012-08-25 00:09:141327}
1328
[email protected]c1bb5af2013-03-13 19:06:271329static void AdjustScrollsForPageScaleChange(LayerImpl* layer_impl,
1330 float page_scale_change) {
1331 if (!layer_impl)
1332 return;
[email protected]94f206c12012-08-25 00:09:141333
[email protected]c1bb5af2013-03-13 19:06:271334 if (layer_impl->scrollable()) {
1335 // We need to convert impl-side scroll deltas to page_scale space.
1336 gfx::Vector2dF scroll_delta = layer_impl->scroll_delta();
1337 scroll_delta.Scale(page_scale_change);
1338 layer_impl->SetScrollDelta(scroll_delta);
1339 }
[email protected]94f206c12012-08-25 00:09:141340
[email protected]c1bb5af2013-03-13 19:06:271341 for (size_t i = 0; i < layer_impl->children().size(); ++i)
1342 AdjustScrollsForPageScaleChange(layer_impl->children()[i],
1343 page_scale_change);
[email protected]94f206c12012-08-25 00:09:141344}
1345
[email protected]d9083762013-03-24 01:36:401346void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1347 if (overdraw_bottom_height == overdraw_bottom_height_)
1348 return;
1349 overdraw_bottom_height_ = overdraw_bottom_height;
1350
1351 UpdateMaxScrollOffset();
1352}
1353
[email protected]c1bb5af2013-03-13 19:06:271354void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1355 if (device_scale_factor == device_scale_factor_)
1356 return;
1357 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271358
[email protected]c1bb5af2013-03-13 19:06:271359 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141360}
1361
[email protected]c1bb5af2013-03-13 19:06:271362void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1363 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141364}
1365
[email protected]c1bb5af2013-03-13 19:06:271366void LayerTreeHostImpl::setActiveTreeNeedsUpdateDrawProperties() {
1367 active_tree_->set_needs_update_draw_properties();
[email protected]3ba4cae2013-01-16 03:58:381368}
1369
[email protected]c1bb5af2013-03-13 19:06:271370void LayerTreeHostImpl::setNeedsRedraw() {
1371 client_->SetNeedsRedrawOnImplThread();
[email protected]94f206c12012-08-25 00:09:141372}
1373
[email protected]c1bb5af2013-03-13 19:06:271374bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
1375 active_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_ACTIVE_TREE);
1376 return active_tree_->RenderSurfaceLayerList().size();
[email protected]94f206c12012-08-25 00:09:141377}
1378
[email protected]c1bb5af2013-03-13 19:06:271379InputHandlerClient::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1380 gfx::Point viewport_point, InputHandlerClient::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291381 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141382
[email protected]c1bb5af2013-03-13 19:06:271383 if (top_controls_manager_)
1384 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381385
[email protected]c1bb5af2013-03-13 19:06:271386 DCHECK(!CurrentlyScrollingLayer());
1387 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141388
[email protected]c1bb5af2013-03-13 19:06:271389 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141390 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141391
[email protected]c1bb5af2013-03-13 19:06:271392 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1393 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141394
[email protected]c1bb5af2013-03-13 19:06:271395 // First find out which layer was hit from the saved list of visible layers
1396 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391397 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271398 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521399
[email protected]c1bb5af2013-03-13 19:06:271400 // Walk up the hierarchy and look for a scrollable layer.
1401 LayerImpl* potentially_scrolling_layer_impl = 0;
1402 for (; layer_impl; layer_impl = layer_impl->parent()) {
1403 // The content layer can also block attempts to scroll outside the main
1404 // thread.
1405 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1406 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431407 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271408 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
[email protected]b7c4783f2013-03-15 23:11:421409 active_tree()->DidBeginScroll();
[email protected]c1bb5af2013-03-13 19:06:271410 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141411 }
1412
[email protected]c1bb5af2013-03-13 19:06:271413 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1414 if (!scroll_layer_impl)
1415 continue;
[email protected]94f206c12012-08-25 00:09:141416
[email protected]c1bb5af2013-03-13 19:06:271417 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141418
[email protected]c1bb5af2013-03-13 19:06:271419 // If any layer wants to divert the scroll event to the main thread, abort.
1420 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431421 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271422 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
[email protected]b7c4783f2013-03-15 23:11:421423 active_tree()->DidBeginScroll();
[email protected]c1bb5af2013-03-13 19:06:271424 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141425 }
1426
[email protected]c1bb5af2013-03-13 19:06:271427 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1428 potentially_scrolling_layer_impl = scroll_layer_impl;
1429 }
1430
1431 // When hiding top controls is enabled and the controls are hidden or
1432 // overlaying the content, force scrolls to be enabled on the root layer to
1433 // allow bringing the top controls back into view.
1434 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1435 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521436 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271437 potentially_scrolling_layer_impl = RootScrollLayer();
1438 }
1439
1440 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201441 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271442 potentially_scrolling_layer_impl);
1443 should_bubble_scrolls_ = (type != NonBubblingGesture);
1444 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431445 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271446 client_->RenewTreePriority();
1447 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
[email protected]b7c4783f2013-03-15 23:11:421448 active_tree()->DidBeginScroll();
[email protected]c1bb5af2013-03-13 19:06:271449 return ScrollStarted;
1450 }
1451 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141452}
1453
[email protected]c1bb5af2013-03-13 19:06:271454gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1455 LayerImpl* layer_impl,
1456 float scale_from_viewport_to_screen_space,
1457 gfx::PointF viewport_point,
1458 gfx::Vector2dF viewport_delta) {
1459 // Layers with non-invertible screen space transforms should not have passed
1460 // the scroll hit test in the first place.
1461 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1462 gfx::Transform inverse_screen_space_transform(
1463 gfx::Transform::kSkipInitialization);
1464 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1465 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351466 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1467 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271468 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141469
[email protected]c1bb5af2013-03-13 19:06:271470 gfx::PointF screen_space_point =
1471 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141472
[email protected]c1bb5af2013-03-13 19:06:271473 gfx::Vector2dF screen_space_delta = viewport_delta;
1474 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1475
1476 // First project the scroll start and end points to local layer space to find
1477 // the scroll delta in layer coordinates.
1478 bool start_clipped, end_clipped;
1479 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1480 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211481 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271482 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211483 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271484 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211485 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271486 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211487 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271488
1489 // In general scroll point coordinates should not get clipped.
1490 DCHECK(!start_clipped);
1491 DCHECK(!end_clipped);
1492 if (start_clipped || end_clipped)
1493 return gfx::Vector2dF();
1494
1495 // local_start_point and local_end_point are in content space but we want to
1496 // move them to layer space for scrolling.
1497 float width_scale = 1.f / layer_impl->contents_scale_x();
1498 float height_scale = 1.f / layer_impl->contents_scale_y();
1499 local_start_point.Scale(width_scale, height_scale);
1500 local_end_point.Scale(width_scale, height_scale);
1501
1502 // Apply the scroll delta.
1503 gfx::Vector2dF previous_delta = layer_impl->scroll_delta();
1504 layer_impl->ScrollBy(local_end_point - local_start_point);
1505
1506 // Get the end point in the layer's content space so we can apply its
1507 // ScreenSpaceTransform.
1508 gfx::PointF actual_local_end_point = local_start_point +
1509 layer_impl->scroll_delta() -
1510 previous_delta;
1511 gfx::PointF actual_local_content_end_point =
1512 gfx::ScalePoint(actual_local_end_point,
1513 1.f / width_scale,
1514 1.f / height_scale);
1515
1516 // Calculate the applied scroll delta in viewport space coordinates.
1517 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211518 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271519 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211520 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271521 DCHECK(!end_clipped);
1522 if (end_clipped)
1523 return gfx::Vector2dF();
1524 gfx::PointF actual_viewport_end_point =
1525 gfx::ScalePoint(actual_screen_space_end_point,
1526 1.f / scale_from_viewport_to_screen_space);
1527 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141528}
1529
[email protected]c1bb5af2013-03-13 19:06:271530static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1531 gfx::Vector2dF local_delta) {
1532 gfx::Vector2dF previous_delta(layer_impl->scroll_delta());
1533 layer_impl->ScrollBy(local_delta);
1534 return layer_impl->scroll_delta() - previous_delta;
1535}
1536
1537bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1538 gfx::Vector2dF scroll_delta) {
1539 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1540 if (!CurrentlyScrollingLayer())
1541 return false;
1542
1543 gfx::Vector2dF pending_delta = scroll_delta;
1544 bool did_scroll = false;
[email protected]60b4d252013-03-23 18:49:421545 bool consume_by_top_controls = top_controls_manager_ &&
1546 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061547
[email protected]c1bb5af2013-03-13 19:06:271548 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1549 layer_impl;
1550 layer_impl = layer_impl->parent()) {
1551 if (!layer_impl->scrollable())
1552 continue;
1553
[email protected]60b4d252013-03-23 18:49:421554 // Only allow bubble scrolling when the scroll is in the direction to make
1555 // the top controls visible.
1556 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1557 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1558 UpdateMaxScrollOffset();
1559 }
1560
[email protected]c1bb5af2013-03-13 19:06:271561 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271562 // Gesture events need to be transformed from viewport coordinates to local
1563 // layer coordinates so that the scrolling contents exactly follow the
1564 // user's finger. In contrast, wheel events represent a fixed amount of
1565 // scrolling so we can just apply them directly.
1566 if (!wheel_scrolling_) {
1567 float scale_from_viewport_to_screen_space = device_scale_factor_;
1568 applied_delta =
1569 ScrollLayerWithViewportSpaceDelta(layer_impl,
1570 scale_from_viewport_to_screen_space,
1571 viewport_point, pending_delta);
1572 } else {
1573 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141574 }
[email protected]94f206c12012-08-25 00:09:141575
[email protected]c1bb5af2013-03-13 19:06:271576 // If the layer wasn't able to move, try the next one in the hierarchy.
1577 float move_threshold_squared = 0.1f * 0.1f;
1578 if (applied_delta.LengthSquared() < move_threshold_squared) {
1579 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1580 continue;
1581 else
1582 break;
[email protected]94f206c12012-08-25 00:09:141583 }
[email protected]c1bb5af2013-03-13 19:06:271584 did_scroll = true;
1585 did_lock_scrolling_layer_ = true;
1586 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:201587 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:271588 break;
[email protected]94f206c12012-08-25 00:09:141589 }
[email protected]94f206c12012-08-25 00:09:141590
[email protected]c1bb5af2013-03-13 19:06:271591 // If the applied delta is within 45 degrees of the input delta, bail out to
1592 // make it easier to scroll just one layer in one direction without
1593 // affecting any of its parents.
1594 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:211595 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:271596 applied_delta, pending_delta) < angle_threshold) {
1597 pending_delta = gfx::Vector2d();
1598 break;
[email protected]4a23c374c2012-12-08 08:38:551599 }
[email protected]c1bb5af2013-03-13 19:06:271600
1601 // Allow further movement only on an axis perpendicular to the direction in
1602 // which the layer moved.
1603 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:211604 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:271605
1606 if (gfx::ToFlooredVector2d(pending_delta).IsZero())
1607 break;
1608 }
1609
[email protected]b7c4783f2013-03-15 23:11:421610 active_tree()->DidUpdateScroll();
[email protected]c1bb5af2013-03-13 19:06:271611 if (did_scroll) {
1612 client_->SetNeedsCommitOnImplThread();
1613 client_->SetNeedsRedrawOnImplThread();
1614 client_->RenewTreePriority();
1615 }
1616 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:551617}
1618
[email protected]be782f52013-03-23 21:36:141619// This implements scrolling by page as described here:
1620// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
1621// for events with WHEEL_PAGESCROLL set.
1622bool LayerTreeHostImpl::ScrollVerticallyByPage(
1623 gfx::Point viewport_point,
1624 WebKit::WebScrollbar::ScrollDirection direction) {
1625 DCHECK(wheel_scrolling_);
1626
1627 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1628 layer_impl;
1629 layer_impl = layer_impl->parent()) {
1630 if (!layer_impl->scrollable())
1631 continue;
1632
1633 if (!layer_impl->vertical_scrollbar_layer())
1634 continue;
1635
1636 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
1637
1638 // These magical values match WebKit and are designed to scroll nearly the
1639 // entire visible content height but leave a bit of overlap.
1640 float page = std::max(height * 0.875f, 1.f);
1641 if (direction == WebKit::WebScrollbar::ScrollBackward)
1642 page = -page;
1643
1644 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
1645
1646 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
1647
1648 if (!applied_delta.IsZero()) {
1649 active_tree()->DidUpdateScroll();
1650 client_->SetNeedsCommitOnImplThread();
1651 client_->SetNeedsRedrawOnImplThread();
1652 client_->RenewTreePriority();
1653 return true;
1654 }
1655
1656 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
1657 }
1658
1659 return false;
1660}
1661
[email protected]c1bb5af2013-03-13 19:06:271662void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
1663 active_tree_->ClearCurrentlyScrollingLayer();
1664 did_lock_scrolling_layer_ = false;
[email protected]94f206c12012-08-25 00:09:141665}
1666
[email protected]c1bb5af2013-03-13 19:06:271667void LayerTreeHostImpl::ScrollEnd() {
1668 if (top_controls_manager_)
1669 top_controls_manager_->ScrollEnd();
1670 ClearCurrentlyScrollingLayer();
[email protected]b7c4783f2013-03-15 23:11:421671 active_tree()->DidEndScroll();
[email protected]0fc818e2013-03-18 06:45:201672 StartScrollbarAnimation(base::TimeTicks::Now());
[email protected]94f206c12012-08-25 00:09:141673}
1674
[email protected]c1bb5af2013-03-13 19:06:271675void LayerTreeHostImpl::PinchGestureBegin() {
1676 pinch_gesture_active_ = true;
1677 previous_pinch_anchor_ = gfx::Point();
1678 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:141679}
1680
[email protected]c1bb5af2013-03-13 19:06:271681void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
1682 gfx::Point anchor) {
1683 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:281684
[email protected]c1bb5af2013-03-13 19:06:271685 if (!RootScrollLayer())
1686 return;
[email protected]d3afa112012-12-08 06:24:281687
[email protected]c1bb5af2013-03-13 19:06:271688 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1689 // position over the course of the magnify.
1690 float page_scale_delta = active_tree_->page_scale_delta();
1691 gfx::PointF previous_scale_anchor =
1692 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1693 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
1694 page_scale_delta = active_tree_->page_scale_delta();
1695 gfx::PointF new_scale_anchor =
1696 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
1697 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
1698
1699 previous_pinch_anchor_ = anchor;
1700
1701 move.Scale(1 / active_tree_->page_scale_factor());
1702
1703 RootScrollLayer()->ScrollBy(move);
1704
[email protected]c1bb5af2013-03-13 19:06:271705 client_->SetNeedsCommitOnImplThread();
1706 client_->SetNeedsRedrawOnImplThread();
1707 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:281708}
1709
[email protected]c1bb5af2013-03-13 19:06:271710void LayerTreeHostImpl::PinchGestureEnd() {
1711 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:271712 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141713}
1714
[email protected]c1bb5af2013-03-13 19:06:271715static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
1716 LayerImpl* layer_impl) {
1717 if (!layer_impl)
1718 return;
[email protected]94f206c12012-08-25 00:09:141719
[email protected]c1bb5af2013-03-13 19:06:271720 gfx::Vector2d scroll_delta =
1721 gfx::ToFlooredVector2d(layer_impl->scroll_delta());
1722 if (!scroll_delta.IsZero()) {
1723 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:391724 scroll.layer_id = layer_impl->id();
1725 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:271726 scroll_info->scrolls.push_back(scroll);
1727 layer_impl->SetSentScrollDelta(scroll_delta);
1728 }
[email protected]94f206c12012-08-25 00:09:141729
[email protected]c1bb5af2013-03-13 19:06:271730 for (size_t i = 0; i < layer_impl->children().size(); ++i)
1731 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:141732}
1733
[email protected]c1bb5af2013-03-13 19:06:271734scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
1735 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:301736
[email protected]c1bb5af2013-03-13 19:06:271737 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:391738 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
1739 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:301740
[email protected]c1bb5af2013-03-13 19:06:271741 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:301742}
1743
[email protected]c1bb5af2013-03-13 19:06:271744void LayerTreeHostImpl::SetFullRootLayerDamage() {
1745 if (active_tree_->root_layer()) {
1746 RenderSurfaceImpl* render_surface =
1747 active_tree_->root_layer()->render_surface();
1748 if (render_surface)
1749 render_surface->damage_tracker()->ForceFullDamageNextUpdate();
1750 }
[email protected]829ad972013-01-28 23:36:101751}
1752
[email protected]c1bb5af2013-03-13 19:06:271753void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
1754 if (!page_scale_animation_ || !RootScrollLayer())
1755 return;
1756
1757 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
1758 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
1759 RootScrollLayer()->scroll_delta();
1760
1761 active_tree_->SetPageScaleDelta(
1762 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
1763 active_tree_->page_scale_factor());
1764 gfx::Vector2dF next_scroll =
1765 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
1766
1767 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
1768 client_->SetNeedsRedrawOnImplThread();
1769
1770 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
1771 page_scale_animation_.reset();
1772 client_->SetNeedsCommitOnImplThread();
1773 client_->RenewTreePriority();
1774 }
[email protected]829ad972013-01-28 23:36:101775}
1776
[email protected]ffb2720f2013-03-15 19:18:371777void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
1778 if (!top_controls_manager_ || !RootScrollLayer())
1779 return;
1780 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
1781 UpdateMaxScrollOffset();
1782 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
1783 scroll, 1.f / active_tree_->total_page_scale_factor()));
1784}
1785
[email protected]c1bb5af2013-03-13 19:06:271786void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
1787 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:521788 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:271789 animation_registrar_->active_animation_controllers().empty() ||
1790 !active_tree_->root_layer())
1791 return;
1792
1793 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
1794
1795 last_animation_time_ = wall_clock_time;
1796 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
1797
1798 AnimationRegistrar::AnimationControllerMap copy =
1799 animation_registrar_->active_animation_controllers();
1800 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
1801 iter != copy.end();
1802 ++iter)
1803 (*iter).second->Animate(monotonic_seconds);
1804
1805 client_->SetNeedsRedrawOnImplThread();
1806 SetBackgroundTickingEnabled(
1807 !visible_ &&
1808 !animation_registrar_->active_animation_controllers().empty());
[email protected]131a0c22013-02-12 18:31:081809}
1810
[email protected]c1bb5af2013-03-13 19:06:271811void LayerTreeHostImpl::UpdateAnimationState() {
[email protected]8e0176d2013-03-21 03:14:521812 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:271813 animation_registrar_->active_animation_controllers().empty() ||
1814 !active_tree_->root_layer())
1815 return;
1816
1817 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
1818 scoped_ptr<AnimationEventsVector> events =
1819 make_scoped_ptr(new AnimationEventsVector);
1820 AnimationRegistrar::AnimationControllerMap copy =
1821 animation_registrar_->active_animation_controllers();
1822 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
1823 iter != copy.end();
1824 ++iter)
1825 (*iter).second->UpdateState(events.get());
1826
1827 if (!events->empty()) {
1828 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
1829 last_animation_time_);
1830 }
[email protected]131a0c22013-02-12 18:31:081831}
1832
[email protected]c1bb5af2013-03-13 19:06:271833base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
1834 return base::TimeDelta::FromSeconds(1);
1835}
1836
1837void LayerTreeHostImpl::SendDidLoseOutputSurfaceRecursive(LayerImpl* current) {
1838 DCHECK(current);
1839 current->DidLoseOutputSurface();
1840 if (current->mask_layer())
1841 SendDidLoseOutputSurfaceRecursive(current->mask_layer());
1842 if (current->replica_layer())
1843 SendDidLoseOutputSurfaceRecursive(current->replica_layer());
1844 for (size_t i = 0; i < current->children().size(); ++i)
1845 SendDidLoseOutputSurfaceRecursive(current->children()[i]);
1846}
1847
1848void LayerTreeHostImpl::ClearRenderSurfaces() {
1849 active_tree_->ClearRenderSurfaces();
1850 if (pending_tree_)
1851 pending_tree_->ClearRenderSurfaces();
1852}
1853
1854std::string LayerTreeHostImpl::LayerTreeAsText() const {
1855 std::string str;
1856 if (active_tree_->root_layer()) {
1857 str = active_tree_->root_layer()->LayerTreeAsText();
1858 str += "RenderSurfaces:\n";
1859 DumpRenderSurfaces(&str, 1, active_tree_->root_layer());
1860 }
1861 return str;
1862}
1863
1864std::string LayerTreeHostImpl::LayerTreeAsJson() const {
1865 std::string str;
1866 if (active_tree_->root_layer()) {
1867 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
1868 base::JSONWriter::WriteWithOptions(
1869 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
1870 }
1871 return str;
1872}
1873
1874void LayerTreeHostImpl::DumpRenderSurfaces(std::string* str,
1875 int indent,
1876 const LayerImpl* layer) const {
1877 if (layer->render_surface())
1878 layer->render_surface()->DumpSurface(str, indent);
1879
1880 for (size_t i = 0; i < layer->children().size(); ++i)
1881 DumpRenderSurfaces(str, indent, layer->children()[i]);
1882}
1883
1884int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:361885 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:271886}
1887
[email protected]c1bb5af2013-03-13 19:06:271888void LayerTreeHostImpl::SendManagedMemoryStats(
1889 size_t memory_visible_bytes,
1890 size_t memory_visible_and_nearby_bytes,
1891 size_t memory_use_bytes) {
1892 if (!renderer_)
1893 return;
1894
1895 // Round the numbers being sent up to the next 8MB, to throttle the rate
1896 // at which we spam the GPU process.
1897 static const size_t rounding_step = 8 * 1024 * 1024;
1898 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
1899 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
1900 rounding_step);
1901 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
1902 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
1903 last_sent_memory_visible_and_nearby_bytes_ ==
1904 memory_visible_and_nearby_bytes &&
1905 last_sent_memory_use_bytes_ == memory_use_bytes) {
1906 return;
1907 }
1908 last_sent_memory_visible_bytes_ = memory_visible_bytes;
1909 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
1910 last_sent_memory_use_bytes_ = memory_use_bytes;
1911
1912 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
1913 last_sent_memory_visible_and_nearby_bytes_,
1914 last_sent_memory_use_bytes_);
1915}
1916
1917void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
1918 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
1919}
1920
1921void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
1922 base::TimeTicks time) {
1923 if (!layer)
1924 return;
1925
1926 ScrollbarAnimationController* scrollbar_controller =
1927 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:351928 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:201929 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:421930 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
1931 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:271932 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:201933 }
[email protected]c1bb5af2013-03-13 19:06:271934
1935 for (size_t i = 0; i < layer->children().size(); ++i)
1936 AnimateScrollbarsRecursive(layer->children()[i], time);
1937}
1938
[email protected]0fc818e2013-03-18 06:45:201939void LayerTreeHostImpl::StartScrollbarAnimation(base::TimeTicks time) {
1940 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
1941 StartScrollbarAnimationRecursive(RootLayer(), time);
1942}
1943
1944void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
1945 base::TimeTicks time) {
1946 if (!layer)
1947 return;
1948
1949 ScrollbarAnimationController* scrollbar_controller =
1950 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:351951 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
1952 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:201953 if (delay > base::TimeDelta())
1954 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:351955 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:201956 client_->SetNeedsRedrawOnImplThread();
1957 }
1958
1959 for (size_t i = 0; i < layer->children().size(); ++i)
1960 StartScrollbarAnimationRecursive(layer->children()[i], time);
1961}
1962
[email protected]c1bb5af2013-03-13 19:06:271963void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
1964 if (!tile_manager_)
1965 return;
1966
1967 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
1968 if (new_state.tree_priority == priority)
1969 return;
1970
1971 new_state.tree_priority = priority;
1972 tile_manager_->SetGlobalState(new_state);
1973}
1974
1975void LayerTreeHostImpl::BeginNextFrame() {
1976 current_frame_time_ = base::TimeTicks();
1977}
1978
1979base::TimeTicks LayerTreeHostImpl::CurrentFrameTime() {
1980 if (current_frame_time_.is_null())
1981 current_frame_time_ = base::TimeTicks::Now();
1982 return current_frame_time_;
1983}
1984
1985scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
1986 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
1987 state->Set("activation_state", ActivationStateAsValue().release());
1988 state->Set("frame_state", FrameStateAsValue().release());
1989 return state.PassAs<base::Value>();
1990}
1991
1992scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
1993 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]7d3cbc92013-03-18 22:33:041994 state->SetString("lthi_id", base::StringPrintf("%p", this));
[email protected]c1bb5af2013-03-13 19:06:271995 state->SetBoolean("visible_resources_ready",
1996 pending_tree_->AreVisibleResourcesReady());
1997 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
1998 return state.PassAs<base::Value>();
1999}
2000
2001scoped_ptr<base::Value> LayerTreeHostImpl::FrameStateAsValue() const {
2002 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]7d3cbc92013-03-18 22:33:042003 state->SetString("lthi_id", base::StringPrintf("%p", this));
[email protected]c1bb5af2013-03-13 19:06:272004 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212005 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272006 if (tile_manager_)
2007 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2008 state->Set("active_tree", active_tree_->AsValue().release());
2009 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082010}
2011
[email protected]b9dcf43a2013-01-09 00:15:292012// static
[email protected]c1bb5af2013-03-13 19:06:272013LayerImpl* LayerTreeHostImpl::GetNonCompositedContentLayerRecursive(
2014 LayerImpl* layer) {
2015 if (!layer)
[email protected]b9dcf43a2013-01-09 00:15:292016 return NULL;
[email protected]c1bb5af2013-03-13 19:06:272017
2018 if (layer->DrawsContent())
2019 return layer;
2020
2021 for (LayerImpl::LayerList::const_iterator it = layer->children().begin();
2022 it != layer->children().end(); ++it) {
2023 LayerImpl* nccr = GetNonCompositedContentLayerRecursive(*it);
2024 if (nccr)
2025 return nccr;
2026 }
2027
2028 return NULL;
[email protected]b9dcf43a2013-01-09 00:15:292029}
2030
[email protected]c1bb5af2013-03-13 19:06:272031skia::RefPtr<SkPicture> LayerTreeHostImpl::CapturePicture() {
2032 LayerTreeImpl* tree =
2033 pending_tree_ ? pending_tree_.get() : active_tree_.get();
2034 LayerImpl* layer = GetNonCompositedContentLayerRecursive(tree->root_layer());
2035 return layer ? layer->GetPicture() : skia::RefPtr<SkPicture>();
[email protected]b9dcf43a2013-01-09 00:15:292036}
2037
[email protected]c1bb5af2013-03-13 19:06:272038void LayerTreeHostImpl::SetDebugState(const LayerTreeDebugState& debug_state) {
[email protected]846f455b2013-03-18 19:07:412039 if (debug_state_.continuous_painting != debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272040 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242041
[email protected]c1bb5af2013-03-13 19:06:272042 debug_state_ = debug_state;
[email protected]d0d12192013-02-08 19:02:022043}
2044
[email protected]d3143c732012-10-05 19:17:592045} // namespace cc