1 // 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.
5 #include "cc/trees/layer_tree_impl.h"
10 #include "base/debug/trace_event.h"
11 #include "cc/animation/keyframed_animation_curve.h"
12 #include "cc/animation/scrollbar_animation_controller.h"
13 #include "cc/animation/scrollbar_animation_controller_linear_fade.h"
14 #include "cc/animation/scrollbar_animation_controller_thinning.h"
15 #include "cc/base/math_util.h"
16 #include "cc/base/util.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/heads_up_display_layer_impl.h"
19 #include "cc/layers/layer.h"
20 #include "cc/layers/layer_iterator.h"
21 #include "cc/layers/render_surface_impl.h"
22 #include "cc/layers/scrollbar_layer_impl_base.h"
23 #include "cc/resources/ui_resource_request.h"
24 #include "cc/trees/layer_tree_host_common.h"
25 #include "cc/trees/layer_tree_host_impl.h"
26 #include "ui/gfx/point_conversions.h"
27 #include "ui/gfx/size_conversions.h"
28 #include "ui/gfx/vector2d_conversions.h"
32 // This class exists to split the LayerScrollOffsetDelegate between the
33 // InnerViewportScrollLayer and the OuterViewportScrollLayer in a manner
34 // that never requires the embedder or LayerImpl to know about.
35 class LayerScrollOffsetDelegateProxy
: public LayerImpl::ScrollOffsetDelegate
{
37 LayerScrollOffsetDelegateProxy(LayerImpl
* layer
,
38 LayerScrollOffsetDelegate
* delegate
,
39 LayerTreeImpl
* layer_tree
)
40 : layer_(layer
), delegate_(delegate
), layer_tree_impl_(layer_tree
) {}
41 virtual ~LayerScrollOffsetDelegateProxy() {}
43 gfx::Vector2dF
last_set_scroll_offset() const {
44 return last_set_scroll_offset_
;
47 // LayerScrollOffsetDelegate implementation.
48 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_offset
) OVERRIDE
{
49 last_set_scroll_offset_
= new_offset
;
50 layer_tree_impl_
->UpdateScrollOffsetDelegate();
53 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
54 return layer_tree_impl_
->GetDelegatedScrollOffset(layer_
);
57 virtual bool IsExternalFlingActive() const OVERRIDE
{
58 return delegate_
->IsExternalFlingActive();
63 LayerScrollOffsetDelegate
* delegate_
;
64 LayerTreeImpl
* layer_tree_impl_
;
65 gfx::Vector2dF last_set_scroll_offset_
;
68 LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl
* layer_tree_host_impl
)
69 : layer_tree_host_impl_(layer_tree_host_impl
),
70 source_frame_number_(-1),
72 currently_scrolling_layer_(NULL
),
73 root_layer_scroll_offset_delegate_(NULL
),
75 has_transparent_background_(false),
76 page_scale_layer_(NULL
),
77 inner_viewport_scroll_layer_(NULL
),
78 outer_viewport_scroll_layer_(NULL
),
79 page_scale_factor_(1),
81 sent_page_scale_delta_(1),
82 min_page_scale_factor_(0),
83 max_page_scale_factor_(0),
84 scrolling_layer_id_from_previous_tree_(0),
85 contents_textures_purged_(false),
86 requires_high_res_to_draw_(false),
87 viewport_size_invalid_(false),
88 needs_update_draw_properties_(true),
89 needs_full_tree_sync_(true),
90 next_activation_forces_redraw_(false),
91 render_surface_layer_list_id_(0) {
94 LayerTreeImpl::~LayerTreeImpl() {
95 // Need to explicitly clear the tree prior to destroying this so that
96 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
98 DCHECK(layers_with_copy_output_request_
.empty());
101 void LayerTreeImpl::Shutdown() { root_layer_
.reset(); }
103 void LayerTreeImpl::ReleaseResources() {
105 ReleaseResourcesRecursive(root_layer_
.get());
108 void LayerTreeImpl::SetRootLayer(scoped_ptr
<LayerImpl
> layer
) {
109 if (inner_viewport_scroll_layer_
)
110 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
111 if (outer_viewport_scroll_layer_
)
112 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
113 inner_viewport_scroll_delegate_proxy_
.reset();
114 outer_viewport_scroll_delegate_proxy_
.reset();
116 root_layer_
= layer
.Pass();
117 currently_scrolling_layer_
= NULL
;
118 inner_viewport_scroll_layer_
= NULL
;
119 outer_viewport_scroll_layer_
= NULL
;
120 page_scale_layer_
= NULL
;
122 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
125 LayerImpl
* LayerTreeImpl::InnerViewportScrollLayer() const {
126 return inner_viewport_scroll_layer_
;
129 LayerImpl
* LayerTreeImpl::OuterViewportScrollLayer() const {
130 return outer_viewport_scroll_layer_
;
133 gfx::Vector2dF
LayerTreeImpl::TotalScrollOffset() const {
134 gfx::Vector2dF offset
;
136 if (inner_viewport_scroll_layer_
)
137 offset
+= inner_viewport_scroll_layer_
->TotalScrollOffset();
139 if (outer_viewport_scroll_layer_
)
140 offset
+= outer_viewport_scroll_layer_
->TotalScrollOffset();
145 gfx::Vector2dF
LayerTreeImpl::TotalMaxScrollOffset() const {
146 gfx::Vector2dF offset
;
148 if (inner_viewport_scroll_layer_
)
149 offset
+= inner_viewport_scroll_layer_
->MaxScrollOffset();
151 if (outer_viewport_scroll_layer_
)
152 offset
+= outer_viewport_scroll_layer_
->MaxScrollOffset();
156 gfx::Vector2dF
LayerTreeImpl::TotalScrollDelta() const {
157 DCHECK(inner_viewport_scroll_layer_
);
158 gfx::Vector2dF delta
= inner_viewport_scroll_layer_
->ScrollDelta();
160 if (outer_viewport_scroll_layer_
)
161 delta
+= outer_viewport_scroll_layer_
->ScrollDelta();
166 scoped_ptr
<LayerImpl
> LayerTreeImpl::DetachLayerTree() {
167 // Clear all data structures that have direct references to the layer tree.
168 scrolling_layer_id_from_previous_tree_
=
169 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0;
170 if (inner_viewport_scroll_layer_
)
171 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
172 if (outer_viewport_scroll_layer_
)
173 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
174 inner_viewport_scroll_delegate_proxy_
.reset();
175 outer_viewport_scroll_delegate_proxy_
.reset();
176 inner_viewport_scroll_layer_
= NULL
;
177 outer_viewport_scroll_layer_
= NULL
;
178 page_scale_layer_
= NULL
;
179 currently_scrolling_layer_
= NULL
;
181 render_surface_layer_list_
.clear();
182 set_needs_update_draw_properties();
183 return root_layer_
.Pass();
186 void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl
* target_tree
) {
187 // The request queue should have been processed and does not require a push.
188 DCHECK_EQ(ui_resource_request_queue_
.size(), 0u);
190 if (next_activation_forces_redraw_
) {
191 layer_tree_host_impl_
->SetFullRootLayerDamage();
192 next_activation_forces_redraw_
= false;
195 target_tree
->PassSwapPromises(&swap_promise_list_
);
197 target_tree
->SetPageScaleFactorAndLimits(
198 page_scale_factor(), min_page_scale_factor(), max_page_scale_factor());
199 target_tree
->SetPageScaleDelta(
200 target_tree
->page_scale_delta() / target_tree
->sent_page_scale_delta());
201 target_tree
->set_sent_page_scale_delta(1);
203 if (page_scale_layer_
&& inner_viewport_scroll_layer_
) {
204 target_tree
->SetViewportLayersFromIds(
205 page_scale_layer_
->id(),
206 inner_viewport_scroll_layer_
->id(),
207 outer_viewport_scroll_layer_
? outer_viewport_scroll_layer_
->id()
208 : Layer::INVALID_ID
);
210 target_tree
->ClearViewportLayers();
212 // This should match the property synchronization in
213 // LayerTreeHost::finishCommitOnImplThread().
214 target_tree
->set_source_frame_number(source_frame_number());
215 target_tree
->set_background_color(background_color());
216 target_tree
->set_has_transparent_background(has_transparent_background());
218 if (ContentsTexturesPurged())
219 target_tree
->SetContentsTexturesPurged();
221 target_tree
->ResetContentsTexturesPurged();
223 // Always reset this flag on activation, as we would only have activated
224 // if we were in a good state.
225 target_tree
->ResetRequiresHighResToDraw();
227 if (ViewportSizeInvalid())
228 target_tree
->SetViewportSizeInvalid();
230 target_tree
->ResetViewportSizeInvalid();
233 target_tree
->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl
*>(
234 LayerTreeHostCommon::FindLayerInSubtree(
235 target_tree
->root_layer(), hud_layer()->id())));
237 target_tree
->set_hud_layer(NULL
);
240 LayerImpl
* LayerTreeImpl::InnerViewportContainerLayer() const {
241 return inner_viewport_scroll_layer_
242 ? inner_viewport_scroll_layer_
->scroll_clip_layer()
246 LayerImpl
* LayerTreeImpl::CurrentlyScrollingLayer() const {
247 DCHECK(IsActiveTree());
248 return currently_scrolling_layer_
;
251 void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl
* layer
) {
252 if (currently_scrolling_layer_
== layer
)
255 if (currently_scrolling_layer_
&&
256 currently_scrolling_layer_
->scrollbar_animation_controller())
257 currently_scrolling_layer_
->scrollbar_animation_controller()
259 currently_scrolling_layer_
= layer
;
260 if (layer
&& layer
->scrollbar_animation_controller())
261 layer
->scrollbar_animation_controller()->DidScrollBegin();
264 void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
265 SetCurrentlyScrollingLayer(NULL
);
266 scrolling_layer_id_from_previous_tree_
= 0;
269 float LayerTreeImpl::VerticalAdjust(const int clip_layer_id
) const {
270 LayerImpl
* container_layer
= InnerViewportContainerLayer();
271 if (!container_layer
|| clip_layer_id
!= container_layer
->id())
274 return layer_tree_host_impl_
->VerticalAdjust();
279 void ForceScrollbarParameterUpdateAfterScaleChange(LayerImpl
* current_layer
) {
283 while (current_layer
) {
284 current_layer
->ScrollbarParametersDidChange();
285 current_layer
= current_layer
->parent();
291 void LayerTreeImpl::SetPageScaleFactorAndLimits(float page_scale_factor
,
292 float min_page_scale_factor
, float max_page_scale_factor
) {
293 if (!page_scale_factor
)
296 if (min_page_scale_factor
== min_page_scale_factor_
&&
297 max_page_scale_factor
== max_page_scale_factor_
&&
298 page_scale_factor
== page_scale_factor_
)
301 min_page_scale_factor_
= min_page_scale_factor
;
302 max_page_scale_factor_
= max_page_scale_factor
;
303 page_scale_factor_
= page_scale_factor
;
305 if (root_layer_scroll_offset_delegate_
) {
306 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
308 TotalMaxScrollOffset(),
310 total_page_scale_factor(),
311 this->min_page_scale_factor(),
312 this->max_page_scale_factor());
315 ForceScrollbarParameterUpdateAfterScaleChange(page_scale_layer());
318 void LayerTreeImpl::SetPageScaleDelta(float delta
) {
319 // Clamp to the current min/max limits.
320 float total
= page_scale_factor_
* delta
;
321 if (min_page_scale_factor_
&& total
< min_page_scale_factor_
)
322 delta
= min_page_scale_factor_
/ page_scale_factor_
;
323 else if (max_page_scale_factor_
&& total
> max_page_scale_factor_
)
324 delta
= max_page_scale_factor_
/ page_scale_factor_
;
326 if (delta
== page_scale_delta_
)
329 page_scale_delta_
= delta
;
331 if (IsActiveTree()) {
332 LayerTreeImpl
* pending_tree
= layer_tree_host_impl_
->pending_tree();
334 DCHECK_EQ(1, pending_tree
->sent_page_scale_delta());
335 pending_tree
->SetPageScaleDelta(
336 page_scale_delta_
/ sent_page_scale_delta_
);
340 set_needs_update_draw_properties();
342 if (root_layer_scroll_offset_delegate_
) {
343 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
345 TotalMaxScrollOffset(),
347 total_page_scale_factor(),
348 min_page_scale_factor(),
349 max_page_scale_factor());
353 gfx::SizeF
LayerTreeImpl::ScrollableViewportSize() const {
354 if (outer_viewport_scroll_layer_
)
355 return layer_tree_host_impl_
->UnscaledScrollableViewportSize();
357 return gfx::ScaleSize(
358 layer_tree_host_impl_
->UnscaledScrollableViewportSize(),
359 1.0f
/ total_page_scale_factor());
362 gfx::Rect
LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
363 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
364 ? OuterViewportScrollLayer()
365 : InnerViewportScrollLayer();
366 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
368 LayerImpl
* layer
= root_scroll_layer
->children()[0];
369 return MathUtil::MapEnclosingClippedRect(layer
->screen_space_transform(),
370 gfx::Rect(layer
->content_bounds()));
373 static void ApplySentScrollDeltasFromAbortedCommitTo(LayerImpl
* layer
) {
374 layer
->ApplySentScrollDeltasFromAbortedCommit();
377 void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
378 DCHECK(IsActiveTree());
380 page_scale_factor_
*= sent_page_scale_delta_
;
381 page_scale_delta_
/= sent_page_scale_delta_
;
382 sent_page_scale_delta_
= 1.f
;
387 LayerTreeHostCommon::CallFunctionForSubtree(
388 root_layer(), base::Bind(&ApplySentScrollDeltasFromAbortedCommitTo
));
391 static void ApplyScrollDeltasSinceBeginMainFrameTo(LayerImpl
* layer
) {
392 layer
->ApplyScrollDeltasSinceBeginMainFrame();
395 void LayerTreeImpl::ApplyScrollDeltasSinceBeginMainFrame() {
396 DCHECK(IsPendingTree());
400 LayerTreeHostCommon::CallFunctionForSubtree(
401 root_layer(), base::Bind(&ApplyScrollDeltasSinceBeginMainFrameTo
));
404 void LayerTreeImpl::SetViewportLayersFromIds(
405 int page_scale_layer_id
,
406 int inner_viewport_scroll_layer_id
,
407 int outer_viewport_scroll_layer_id
) {
408 page_scale_layer_
= LayerById(page_scale_layer_id
);
409 DCHECK(page_scale_layer_
);
411 inner_viewport_scroll_layer_
=
412 LayerById(inner_viewport_scroll_layer_id
);
413 DCHECK(inner_viewport_scroll_layer_
);
415 outer_viewport_scroll_layer_
=
416 LayerById(outer_viewport_scroll_layer_id
);
417 DCHECK(outer_viewport_scroll_layer_
||
418 outer_viewport_scroll_layer_id
== Layer::INVALID_ID
);
420 if (!root_layer_scroll_offset_delegate_
)
423 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
424 new LayerScrollOffsetDelegateProxy(inner_viewport_scroll_layer_
,
425 root_layer_scroll_offset_delegate_
,
428 if (outer_viewport_scroll_layer_
)
429 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
430 new LayerScrollOffsetDelegateProxy(outer_viewport_scroll_layer_
,
431 root_layer_scroll_offset_delegate_
,
435 void LayerTreeImpl::ClearViewportLayers() {
436 page_scale_layer_
= NULL
;
437 inner_viewport_scroll_layer_
= NULL
;
438 outer_viewport_scroll_layer_
= NULL
;
441 void LayerTreeImpl::UpdateDrawProperties() {
442 needs_update_draw_properties_
= false;
443 render_surface_layer_list_
.clear();
445 // For max_texture_size.
446 if (!layer_tree_host_impl_
->renderer())
454 "LayerTreeImpl::UpdateDrawProperties",
458 source_frame_number_
);
459 LayerImpl
* page_scale_layer
=
460 page_scale_layer_
? page_scale_layer_
: InnerViewportContainerLayer();
461 bool can_render_to_separate_surface
=
462 !output_surface()->ForcedDrawToSoftwareDevice();
464 ++render_surface_layer_list_id_
;
465 LayerTreeHostCommon::CalcDrawPropsImplInputs
inputs(
468 layer_tree_host_impl_
->DrawTransform(),
469 device_scale_factor(),
470 total_page_scale_factor(),
473 settings().can_use_lcd_text
,
474 can_render_to_separate_surface
,
475 settings().layer_transforms_should_scale_layer_contents
,
476 &render_surface_layer_list_
,
477 render_surface_layer_list_id_
);
478 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
483 "LayerTreeImpl::UpdateTilePriorities",
487 source_frame_number_
);
488 // LayerIterator is used here instead of CallFunctionForSubtree to only
489 // UpdateTilePriorities on layers that will be visible (and thus have valid
490 // draw properties) and not because any ordering is required.
491 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
492 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
493 for (LayerIteratorType it
=
494 LayerIteratorType::Begin(&render_surface_layer_list_
);
497 LayerImpl
* layer
= *it
;
498 if (it
.represents_itself())
499 layer
->UpdateTilePriorities();
501 if (!it
.represents_contributing_render_surface())
504 if (layer
->mask_layer())
505 layer
->mask_layer()->UpdateTilePriorities();
506 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer())
507 layer
->replica_layer()->mask_layer()->UpdateTilePriorities();
511 DCHECK(!needs_update_draw_properties_
) <<
512 "CalcDrawProperties should not set_needs_update_draw_properties()";
515 const LayerImplList
& LayerTreeImpl::RenderSurfaceLayerList() const {
516 // If this assert triggers, then the list is dirty.
517 DCHECK(!needs_update_draw_properties_
);
518 return render_surface_layer_list_
;
521 gfx::Size
LayerTreeImpl::ScrollableSize() const {
522 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
523 ? OuterViewportScrollLayer()
524 : InnerViewportScrollLayer();
525 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
527 return root_scroll_layer
->children()[0]->bounds();
530 LayerImpl
* LayerTreeImpl::LayerById(int id
) {
531 LayerIdMap::iterator iter
= layer_id_map_
.find(id
);
532 return iter
!= layer_id_map_
.end() ? iter
->second
: NULL
;
535 void LayerTreeImpl::RegisterLayer(LayerImpl
* layer
) {
536 DCHECK(!LayerById(layer
->id()));
537 layer_id_map_
[layer
->id()] = layer
;
540 void LayerTreeImpl::UnregisterLayer(LayerImpl
* layer
) {
541 DCHECK(LayerById(layer
->id()));
542 layer_id_map_
.erase(layer
->id());
545 void LayerTreeImpl::PushPersistedState(LayerTreeImpl
* pending_tree
) {
546 pending_tree
->SetCurrentlyScrollingLayer(
547 LayerTreeHostCommon::FindLayerInSubtree(pending_tree
->root_layer(),
548 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0));
551 static void DidBecomeActiveRecursive(LayerImpl
* layer
) {
552 layer
->DidBecomeActive();
553 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
554 DidBecomeActiveRecursive(layer
->children()[i
]);
557 void LayerTreeImpl::DidBecomeActive() {
561 if (scrolling_layer_id_from_previous_tree_
) {
562 currently_scrolling_layer_
= LayerTreeHostCommon::FindLayerInSubtree(
563 root_layer_
.get(), scrolling_layer_id_from_previous_tree_
);
566 DidBecomeActiveRecursive(root_layer());
569 bool LayerTreeImpl::ContentsTexturesPurged() const {
570 return contents_textures_purged_
;
573 void LayerTreeImpl::SetContentsTexturesPurged() {
574 if (contents_textures_purged_
)
576 contents_textures_purged_
= true;
577 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
580 void LayerTreeImpl::ResetContentsTexturesPurged() {
581 if (!contents_textures_purged_
)
583 contents_textures_purged_
= false;
584 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
587 void LayerTreeImpl::SetRequiresHighResToDraw() {
588 requires_high_res_to_draw_
= true;
591 void LayerTreeImpl::ResetRequiresHighResToDraw() {
592 requires_high_res_to_draw_
= false;
595 bool LayerTreeImpl::RequiresHighResToDraw() const {
596 return requires_high_res_to_draw_
;
599 bool LayerTreeImpl::ViewportSizeInvalid() const {
600 return viewport_size_invalid_
;
603 void LayerTreeImpl::SetViewportSizeInvalid() {
604 viewport_size_invalid_
= true;
605 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
608 void LayerTreeImpl::ResetViewportSizeInvalid() {
609 viewport_size_invalid_
= false;
610 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
613 Proxy
* LayerTreeImpl::proxy() const {
614 return layer_tree_host_impl_
->proxy();
617 const LayerTreeSettings
& LayerTreeImpl::settings() const {
618 return layer_tree_host_impl_
->settings();
621 const RendererCapabilitiesImpl
& LayerTreeImpl::GetRendererCapabilities() const {
622 return layer_tree_host_impl_
->GetRendererCapabilities();
625 ContextProvider
* LayerTreeImpl::context_provider() const {
626 return output_surface()->context_provider();
629 OutputSurface
* LayerTreeImpl::output_surface() const {
630 return layer_tree_host_impl_
->output_surface();
633 ResourceProvider
* LayerTreeImpl::resource_provider() const {
634 return layer_tree_host_impl_
->resource_provider();
637 TileManager
* LayerTreeImpl::tile_manager() const {
638 return layer_tree_host_impl_
->tile_manager();
641 FrameRateCounter
* LayerTreeImpl::frame_rate_counter() const {
642 return layer_tree_host_impl_
->fps_counter();
645 PaintTimeCounter
* LayerTreeImpl::paint_time_counter() const {
646 return layer_tree_host_impl_
->paint_time_counter();
649 MemoryHistory
* LayerTreeImpl::memory_history() const {
650 return layer_tree_host_impl_
->memory_history();
653 bool LayerTreeImpl::device_viewport_valid_for_tile_management() const {
654 return layer_tree_host_impl_
->device_viewport_valid_for_tile_management();
657 gfx::Size
LayerTreeImpl::device_viewport_size() const {
658 return layer_tree_host_impl_
->device_viewport_size();
661 bool LayerTreeImpl::IsActiveTree() const {
662 return layer_tree_host_impl_
->active_tree() == this;
665 bool LayerTreeImpl::IsPendingTree() const {
666 return layer_tree_host_impl_
->pending_tree() == this;
669 bool LayerTreeImpl::IsRecycleTree() const {
670 return layer_tree_host_impl_
->recycle_tree() == this;
673 LayerImpl
* LayerTreeImpl::FindActiveTreeLayerById(int id
) {
674 LayerTreeImpl
* tree
= layer_tree_host_impl_
->active_tree();
677 return tree
->LayerById(id
);
680 LayerImpl
* LayerTreeImpl::FindPendingTreeLayerById(int id
) {
681 LayerTreeImpl
* tree
= layer_tree_host_impl_
->pending_tree();
684 return tree
->LayerById(id
);
687 int LayerTreeImpl::MaxTextureSize() const {
688 return layer_tree_host_impl_
->GetRendererCapabilities().max_texture_size
;
691 bool LayerTreeImpl::PinchGestureActive() const {
692 return layer_tree_host_impl_
->pinch_gesture_active();
695 base::TimeTicks
LayerTreeImpl::CurrentFrameTimeTicks() const {
696 return layer_tree_host_impl_
->CurrentFrameTimeTicks();
699 base::TimeDelta
LayerTreeImpl::begin_impl_frame_interval() const {
700 return layer_tree_host_impl_
->begin_impl_frame_interval();
703 void LayerTreeImpl::SetNeedsCommit() {
704 layer_tree_host_impl_
->SetNeedsCommit();
707 gfx::Size
LayerTreeImpl::DrawViewportSize() const {
708 return layer_tree_host_impl_
->DrawViewportSize();
711 scoped_ptr
<ScrollbarAnimationController
>
712 LayerTreeImpl::CreateScrollbarAnimationController(LayerImpl
* scrolling_layer
) {
713 DCHECK(settings().scrollbar_fade_delay_ms
);
714 DCHECK(settings().scrollbar_fade_duration_ms
);
715 base::TimeDelta delay
=
716 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_delay_ms
);
717 base::TimeDelta duration
=
718 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_duration_ms
);
719 switch (settings().scrollbar_animator
) {
720 case LayerTreeSettings::LinearFade
: {
721 return ScrollbarAnimationControllerLinearFade::Create(
722 scrolling_layer
, layer_tree_host_impl_
, delay
, duration
)
723 .PassAs
<ScrollbarAnimationController
>();
725 case LayerTreeSettings::Thinning
: {
726 return ScrollbarAnimationControllerThinning::Create(
727 scrolling_layer
, layer_tree_host_impl_
, delay
, duration
)
728 .PassAs
<ScrollbarAnimationController
>();
730 case LayerTreeSettings::NoAnimator
:
734 return scoped_ptr
<ScrollbarAnimationController
>();
737 void LayerTreeImpl::DidAnimateScrollOffset() {
738 layer_tree_host_impl_
->DidAnimateScrollOffset();
741 bool LayerTreeImpl::use_gpu_rasterization() const {
742 return layer_tree_host_impl_
->use_gpu_rasterization();
745 bool LayerTreeImpl::create_low_res_tiling() const {
746 return layer_tree_host_impl_
->create_low_res_tiling();
749 void LayerTreeImpl::SetNeedsRedraw() {
750 layer_tree_host_impl_
->SetNeedsRedraw();
753 const LayerTreeDebugState
& LayerTreeImpl::debug_state() const {
754 return layer_tree_host_impl_
->debug_state();
757 float LayerTreeImpl::device_scale_factor() const {
758 return layer_tree_host_impl_
->device_scale_factor();
761 DebugRectHistory
* LayerTreeImpl::debug_rect_history() const {
762 return layer_tree_host_impl_
->debug_rect_history();
765 AnimationRegistrar
* LayerTreeImpl::animationRegistrar() const {
766 return layer_tree_host_impl_
->animation_registrar();
769 scoped_ptr
<base::Value
> LayerTreeImpl::AsValue() const {
770 scoped_ptr
<base::DictionaryValue
> state(new base::DictionaryValue());
771 TracedValue::MakeDictIntoImplicitSnapshot(
772 state
.get(), "cc::LayerTreeImpl", this);
774 state
->Set("root_layer", root_layer_
->AsValue().release());
776 scoped_ptr
<base::ListValue
> render_surface_layer_list(new base::ListValue());
777 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
778 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
779 for (LayerIteratorType it
= LayerIteratorType::Begin(
780 &render_surface_layer_list_
); it
!= end
; ++it
) {
781 if (!it
.represents_itself())
783 render_surface_layer_list
->Append(TracedValue::CreateIDRef(*it
).release());
786 state
->Set("render_surface_layer_list",
787 render_surface_layer_list
.release());
788 return state
.PassAs
<base::Value
>();
791 void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
792 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
) {
793 if (root_layer_scroll_offset_delegate_
== root_layer_scroll_offset_delegate
)
796 if (!root_layer_scroll_offset_delegate
) {
797 // Make sure we remove the proxies from their layers before
799 if (InnerViewportScrollLayer())
800 InnerViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
801 if (OuterViewportScrollLayer())
802 OuterViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
803 inner_viewport_scroll_delegate_proxy_
.reset();
804 outer_viewport_scroll_delegate_proxy_
.reset();
807 root_layer_scroll_offset_delegate_
= root_layer_scroll_offset_delegate
;
809 if (root_layer_scroll_offset_delegate_
) {
810 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
812 TotalMaxScrollOffset(),
814 total_page_scale_factor(),
815 min_page_scale_factor(),
816 max_page_scale_factor());
818 if (inner_viewport_scroll_layer_
) {
819 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
820 new LayerScrollOffsetDelegateProxy(InnerViewportScrollLayer(),
821 root_layer_scroll_offset_delegate_
,
823 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(
824 inner_viewport_scroll_delegate_proxy_
.get());
827 if (outer_viewport_scroll_layer_
) {
828 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
829 new LayerScrollOffsetDelegateProxy(OuterViewportScrollLayer(),
830 root_layer_scroll_offset_delegate_
,
832 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(
833 outer_viewport_scroll_delegate_proxy_
.get());
838 void LayerTreeImpl::UpdateScrollOffsetDelegate() {
839 DCHECK(InnerViewportScrollLayer());
840 DCHECK(root_layer_scroll_offset_delegate_
);
842 gfx::Vector2dF offset
=
843 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
845 if (OuterViewportScrollLayer())
846 offset
+= outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
848 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
850 TotalMaxScrollOffset(),
852 total_page_scale_factor(),
853 min_page_scale_factor(),
854 max_page_scale_factor());
857 gfx::Vector2dF
LayerTreeImpl::GetDelegatedScrollOffset(LayerImpl
* layer
) {
858 DCHECK(root_layer_scroll_offset_delegate_
);
859 DCHECK(InnerViewportScrollLayer());
860 if (layer
== InnerViewportScrollLayer() && !OuterViewportScrollLayer())
861 return root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
863 // If we get here, we have both inner/outer viewports, and need to distribute
864 // the scroll offset between them.
865 DCHECK(inner_viewport_scroll_delegate_proxy_
);
866 DCHECK(outer_viewport_scroll_delegate_proxy_
);
867 gfx::Vector2dF inner_viewport_offset
=
868 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
869 gfx::Vector2dF outer_viewport_offset
=
870 outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
872 // It may be nothing has changed.
873 gfx::Vector2dF delegate_offset
=
874 root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
875 if (inner_viewport_offset
+ outer_viewport_offset
== delegate_offset
) {
876 if (layer
== InnerViewportScrollLayer())
877 return inner_viewport_offset
;
879 return outer_viewport_offset
;
882 gfx::Vector2d max_outer_viewport_scroll_offset
=
883 OuterViewportScrollLayer()->MaxScrollOffset();
885 outer_viewport_offset
= delegate_offset
- inner_viewport_offset
;
886 outer_viewport_offset
.SetToMin(max_outer_viewport_scroll_offset
);
887 outer_viewport_offset
.SetToMax(gfx::Vector2d());
889 if (layer
== OuterViewportScrollLayer())
890 return outer_viewport_offset
;
892 inner_viewport_offset
= delegate_offset
- outer_viewport_offset
;
894 return inner_viewport_offset
;
897 void LayerTreeImpl::QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
) {
898 DCHECK(swap_promise
);
899 if (swap_promise_list_
.size() > kMaxQueuedSwapPromiseNumber
)
900 BreakSwapPromises(SwapPromise::SWAP_PROMISE_LIST_OVERFLOW
);
901 swap_promise_list_
.push_back(swap_promise
.Pass());
904 void LayerTreeImpl::PassSwapPromises(
905 ScopedPtrVector
<SwapPromise
>* new_swap_promise
) {
906 swap_promise_list_
.insert_and_take(swap_promise_list_
.end(),
908 new_swap_promise
->clear();
911 void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata
* metadata
) {
912 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
913 swap_promise_list_
[i
]->DidSwap(metadata
);
914 swap_promise_list_
.clear();
917 void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason
) {
918 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
919 swap_promise_list_
[i
]->DidNotSwap(reason
);
920 swap_promise_list_
.clear();
923 void LayerTreeImpl::DidModifyTilePriorities() {
924 layer_tree_host_impl_
->DidModifyTilePriorities();
927 void LayerTreeImpl::set_ui_resource_request_queue(
928 const UIResourceRequestQueue
& queue
) {
929 ui_resource_request_queue_
= queue
;
932 ResourceProvider::ResourceId
LayerTreeImpl::ResourceIdForUIResource(
933 UIResourceId uid
) const {
934 return layer_tree_host_impl_
->ResourceIdForUIResource(uid
);
937 bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid
) const {
938 return layer_tree_host_impl_
->IsUIResourceOpaque(uid
);
941 void LayerTreeImpl::ProcessUIResourceRequestQueue() {
942 while (ui_resource_request_queue_
.size() > 0) {
943 UIResourceRequest req
= ui_resource_request_queue_
.front();
944 ui_resource_request_queue_
.pop_front();
946 switch (req
.GetType()) {
947 case UIResourceRequest::UIResourceCreate
:
948 layer_tree_host_impl_
->CreateUIResource(req
.GetId(), req
.GetBitmap());
950 case UIResourceRequest::UIResourceDelete
:
951 layer_tree_host_impl_
->DeleteUIResource(req
.GetId());
953 case UIResourceRequest::UIResourceInvalidRequest
:
959 // If all UI resource evictions were not recreated by processing this queue,
960 // then another commit is required.
961 if (layer_tree_host_impl_
->EvictedUIResourcesExist())
962 layer_tree_host_impl_
->SetNeedsCommit();
965 void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl
* layer
) {
966 // Only the active tree needs to know about layers with copy requests, as
967 // they are aborted if not serviced during draw.
968 DCHECK(IsActiveTree());
970 // DCHECK(std::find(layers_with_copy_output_request_.begin(),
971 // layers_with_copy_output_request_.end(),
972 // layer) == layers_with_copy_output_request_.end());
973 // TODO(danakj): Remove this once crash is found crbug.com/309777
974 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
975 CHECK(layers_with_copy_output_request_
[i
] != layer
)
976 << i
<< " of " << layers_with_copy_output_request_
.size();
978 layers_with_copy_output_request_
.push_back(layer
);
981 void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
) {
982 // Only the active tree needs to know about layers with copy requests, as
983 // they are aborted if not serviced during draw.
984 DCHECK(IsActiveTree());
986 std::vector
<LayerImpl
*>::iterator it
= std::find(
987 layers_with_copy_output_request_
.begin(),
988 layers_with_copy_output_request_
.end(),
990 DCHECK(it
!= layers_with_copy_output_request_
.end());
991 layers_with_copy_output_request_
.erase(it
);
993 // TODO(danakj): Remove this once crash is found crbug.com/309777
994 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
995 CHECK(layers_with_copy_output_request_
[i
] != layer
)
996 << i
<< " of " << layers_with_copy_output_request_
.size();
1000 const std::vector
<LayerImpl
*>& LayerTreeImpl::LayersWithCopyOutputRequest()
1002 // Only the active tree needs to know about layers with copy requests, as
1003 // they are aborted if not serviced during draw.
1004 DCHECK(IsActiveTree());
1006 return layers_with_copy_output_request_
;
1009 void LayerTreeImpl::ReleaseResourcesRecursive(LayerImpl
* current
) {
1011 current
->ReleaseResources();
1012 if (current
->mask_layer())
1013 ReleaseResourcesRecursive(current
->mask_layer());
1014 if (current
->replica_layer())
1015 ReleaseResourcesRecursive(current
->replica_layer());
1016 for (size_t i
= 0; i
< current
->children().size(); ++i
)
1017 ReleaseResourcesRecursive(current
->children()[i
]);
1020 template <typename LayerType
>
1021 static inline bool LayerClipsSubtree(LayerType
* layer
) {
1022 return layer
->masks_to_bounds() || layer
->mask_layer();
1025 static bool PointHitsRect(
1026 const gfx::PointF
& screen_space_point
,
1027 const gfx::Transform
& local_space_to_screen_space_transform
,
1028 const gfx::RectF
& local_space_rect
,
1029 float* distance_to_camera
) {
1030 // If the transform is not invertible, then assume that this point doesn't hit
1032 gfx::Transform
inverse_local_space_to_screen_space(
1033 gfx::Transform::kSkipInitialization
);
1034 if (!local_space_to_screen_space_transform
.GetInverse(
1035 &inverse_local_space_to_screen_space
))
1038 // Transform the hit test point from screen space to the local space of the
1040 bool clipped
= false;
1041 gfx::Point3F planar_point
= MathUtil::ProjectPoint3D(
1042 inverse_local_space_to_screen_space
, screen_space_point
, &clipped
);
1043 gfx::PointF hit_test_point_in_local_space
=
1044 gfx::PointF(planar_point
.x(), planar_point
.y());
1046 // If ProjectPoint could not project to a valid value, then we assume that
1047 // this point doesn't hit this rect.
1051 if (!local_space_rect
.Contains(hit_test_point_in_local_space
))
1054 if (distance_to_camera
) {
1055 // To compute the distance to the camera, we have to take the planar point
1056 // and pull it back to world space and compute the displacement along the
1058 gfx::Point3F
planar_point_in_screen_space(planar_point
);
1059 local_space_to_screen_space_transform
.TransformPoint(
1060 &planar_point_in_screen_space
);
1061 *distance_to_camera
= planar_point_in_screen_space
.z();
1067 static bool PointHitsRegion(const gfx::PointF
& screen_space_point
,
1068 const gfx::Transform
& screen_space_transform
,
1069 const Region
& layer_space_region
,
1070 float layer_content_scale_x
,
1071 float layer_content_scale_y
) {
1072 // If the transform is not invertible, then assume that this point doesn't hit
1074 gfx::Transform
inverse_screen_space_transform(
1075 gfx::Transform::kSkipInitialization
);
1076 if (!screen_space_transform
.GetInverse(&inverse_screen_space_transform
))
1079 // Transform the hit test point from screen space to the local space of the
1081 bool clipped
= false;
1082 gfx::PointF hit_test_point_in_content_space
= MathUtil::ProjectPoint(
1083 inverse_screen_space_transform
, screen_space_point
, &clipped
);
1084 gfx::PointF hit_test_point_in_layer_space
=
1085 gfx::ScalePoint(hit_test_point_in_content_space
,
1086 1.f
/ layer_content_scale_x
,
1087 1.f
/ layer_content_scale_y
);
1089 // If ProjectPoint could not project to a valid value, then we assume that
1090 // this point doesn't hit this region.
1094 return layer_space_region
.Contains(
1095 gfx::ToRoundedPoint(hit_test_point_in_layer_space
));
1098 static bool PointIsClippedBySurfaceOrClipRect(
1099 const gfx::PointF
& screen_space_point
,
1101 LayerImpl
* current_layer
= layer
;
1103 // Walk up the layer tree and hit-test any render_surfaces and any layer
1104 // clip rects that are active.
1105 while (current_layer
) {
1106 if (current_layer
->render_surface() &&
1109 current_layer
->render_surface()->screen_space_transform(),
1110 current_layer
->render_surface()->content_rect(),
1114 // Note that drawable content rects are actually in target surface space, so
1115 // the transform we have to provide is the target surface's
1116 // screen_space_transform.
1117 LayerImpl
* render_target
= current_layer
->render_target();
1118 if (LayerClipsSubtree(current_layer
) &&
1121 render_target
->render_surface()->screen_space_transform(),
1122 current_layer
->drawable_content_rect(),
1126 current_layer
= current_layer
->parent();
1129 // If we have finished walking all ancestors without having already exited,
1130 // then the point is not clipped by any ancestors.
1134 static bool PointHitsLayer(LayerImpl
* layer
,
1135 const gfx::PointF
& screen_space_point
,
1136 float* distance_to_intersection
) {
1137 gfx::RectF
content_rect(layer
->content_bounds());
1138 if (!PointHitsRect(screen_space_point
,
1139 layer
->screen_space_transform(),
1141 distance_to_intersection
))
1144 // At this point, we think the point does hit the layer, but we need to walk
1145 // up the parents to ensure that the layer was not clipped in such a way
1146 // that the hit point actually should not hit the layer.
1147 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer
))
1150 // Skip the HUD layer.
1151 if (layer
== layer
->layer_tree_impl()->hud_layer())
1157 struct FindClosestMatchingLayerDataForRecursion
{
1158 FindClosestMatchingLayerDataForRecursion()
1159 : closest_match(NULL
),
1160 closest_distance(-std::numeric_limits
<float>::infinity()) {}
1161 LayerImpl
* closest_match
;
1162 // Note that the positive z-axis points towards the camera, so bigger means
1163 // closer in this case, counterintuitively.
1164 float closest_distance
;
1167 template <typename Functor
>
1168 static void FindClosestMatchingLayer(
1169 const gfx::PointF
& screen_space_point
,
1171 const Functor
& func
,
1172 FindClosestMatchingLayerDataForRecursion
* data_for_recursion
) {
1173 for (int i
= layer
->children().size() - 1; i
>= 0; --i
) {
1174 FindClosestMatchingLayer(
1175 screen_space_point
, layer
->children()[i
], func
, data_for_recursion
);
1178 float distance_to_intersection
= 0.f
;
1180 PointHitsLayer(layer
, screen_space_point
, &distance_to_intersection
) &&
1181 ((!data_for_recursion
->closest_match
||
1182 distance_to_intersection
> data_for_recursion
->closest_distance
))) {
1183 data_for_recursion
->closest_distance
= distance_to_intersection
;
1184 data_for_recursion
->closest_match
= layer
;
1188 static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl
* layer
) {
1189 if (!layer
->scrollable())
1191 if (layer
->IsDrawnRenderSurfaceLayerListMember())
1193 if (!layer
->scroll_children())
1195 for (std::set
<LayerImpl
*>::const_iterator it
=
1196 layer
->scroll_children()->begin();
1197 it
!= layer
->scroll_children()->end();
1199 if ((*it
)->IsDrawnRenderSurfaceLayerListMember())
1205 struct FindScrollingLayerFunctor
{
1206 bool operator()(LayerImpl
* layer
) const {
1207 return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
);
1211 LayerImpl
* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint(
1212 const gfx::PointF
& screen_space_point
) {
1213 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1214 FindClosestMatchingLayer(screen_space_point
,
1216 FindScrollingLayerFunctor(),
1217 &data_for_recursion
);
1218 return data_for_recursion
.closest_match
;
1221 struct HitTestVisibleScrollableOrTouchableFunctor
{
1222 bool operator()(LayerImpl
* layer
) const {
1223 return layer
->IsDrawnRenderSurfaceLayerListMember() ||
1224 ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
) ||
1225 !layer
->touch_event_handler_region().IsEmpty() ||
1226 layer
->have_wheel_event_handlers();
1230 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPoint(
1231 const gfx::PointF
& screen_space_point
) {
1232 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1233 FindClosestMatchingLayer(screen_space_point
,
1235 HitTestVisibleScrollableOrTouchableFunctor(),
1236 &data_for_recursion
);
1237 return data_for_recursion
.closest_match
;
1240 static bool LayerHasTouchEventHandlersAt(const gfx::PointF
& screen_space_point
,
1241 LayerImpl
* layer_impl
) {
1242 if (layer_impl
->touch_event_handler_region().IsEmpty())
1245 if (!PointHitsRegion(screen_space_point
,
1246 layer_impl
->screen_space_transform(),
1247 layer_impl
->touch_event_handler_region(),
1248 layer_impl
->contents_scale_x(),
1249 layer_impl
->contents_scale_y()))
1252 // At this point, we think the point does hit the touch event handler region
1253 // on the layer, but we need to walk up the parents to ensure that the layer
1254 // was not clipped in such a way that the hit point actually should not hit
1256 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer_impl
))
1262 struct FindTouchEventLayerFunctor
{
1263 bool operator()(LayerImpl
* layer
) const {
1264 return LayerHasTouchEventHandlersAt(screen_space_point
, layer
);
1266 const gfx::PointF screen_space_point
;
1269 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
1270 const gfx::PointF
& screen_space_point
) {
1271 FindTouchEventLayerFunctor func
= {screen_space_point
};
1272 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1273 FindClosestMatchingLayer(
1274 screen_space_point
, root_layer(), func
, &data_for_recursion
);
1275 return data_for_recursion
.closest_match
;