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 "base/debug/trace_event_argument.h"
12 #include "cc/animation/keyframed_animation_curve.h"
13 #include "cc/animation/scrollbar_animation_controller.h"
14 #include "cc/animation/scrollbar_animation_controller_linear_fade.h"
15 #include "cc/animation/scrollbar_animation_controller_thinning.h"
16 #include "cc/base/math_util.h"
17 #include "cc/base/util.h"
18 #include "cc/debug/devtools_instrumentation.h"
19 #include "cc/debug/traced_value.h"
20 #include "cc/input/page_scale_animation.h"
21 #include "cc/layers/heads_up_display_layer_impl.h"
22 #include "cc/layers/layer.h"
23 #include "cc/layers/layer_iterator.h"
24 #include "cc/layers/render_surface_impl.h"
25 #include "cc/layers/scrollbar_layer_impl_base.h"
26 #include "cc/resources/ui_resource_request.h"
27 #include "cc/trees/layer_tree_host_common.h"
28 #include "cc/trees/layer_tree_host_impl.h"
29 #include "cc/trees/occlusion_tracker.h"
30 #include "ui/gfx/geometry/point_conversions.h"
31 #include "ui/gfx/geometry/size_conversions.h"
32 #include "ui/gfx/geometry/vector2d_conversions.h"
36 // This class exists to split the LayerScrollOffsetDelegate between the
37 // InnerViewportScrollLayer and the OuterViewportScrollLayer in a manner
38 // that never requires the embedder or LayerImpl to know about.
39 class LayerScrollOffsetDelegateProxy
: public LayerImpl::ScrollOffsetDelegate
{
41 LayerScrollOffsetDelegateProxy(LayerImpl
* layer
,
42 LayerScrollOffsetDelegate
* delegate
,
43 LayerTreeImpl
* layer_tree
)
44 : layer_(layer
), delegate_(delegate
), layer_tree_impl_(layer_tree
) {}
45 virtual ~LayerScrollOffsetDelegateProxy() {}
47 gfx::ScrollOffset
last_set_scroll_offset() const {
48 return last_set_scroll_offset_
;
51 // LayerScrollOffsetDelegate implementation.
52 void SetTotalScrollOffset(const gfx::ScrollOffset
& new_offset
) override
{
53 last_set_scroll_offset_
= new_offset
;
56 gfx::ScrollOffset
GetTotalScrollOffset() override
{
57 return layer_tree_impl_
->GetDelegatedScrollOffset(layer_
);
60 bool IsExternalFlingActive() const override
{
61 return delegate_
->IsExternalFlingActive();
64 void Update() const override
{
65 layer_tree_impl_
->UpdateScrollOffsetDelegate();
70 LayerScrollOffsetDelegate
* delegate_
;
71 LayerTreeImpl
* layer_tree_impl_
;
72 gfx::ScrollOffset last_set_scroll_offset_
;
75 LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl
* layer_tree_host_impl
)
76 : layer_tree_host_impl_(layer_tree_host_impl
),
77 source_frame_number_(-1),
79 currently_scrolling_layer_(NULL
),
80 root_layer_scroll_offset_delegate_(NULL
),
82 has_transparent_background_(false),
83 page_scale_layer_(NULL
),
84 inner_viewport_scroll_layer_(NULL
),
85 outer_viewport_scroll_layer_(NULL
),
86 page_scale_factor_(1),
88 sent_page_scale_delta_(1),
89 min_page_scale_factor_(0),
90 max_page_scale_factor_(0),
91 scrolling_layer_id_from_previous_tree_(0),
92 contents_textures_purged_(false),
93 viewport_size_invalid_(false),
94 needs_update_draw_properties_(true),
95 needs_full_tree_sync_(true),
96 next_activation_forces_redraw_(false),
97 has_ever_been_drawn_(false),
98 render_surface_layer_list_id_(0),
99 top_controls_layout_height_(0),
100 top_controls_content_offset_(0),
101 top_controls_delta_(0),
102 sent_top_controls_delta_(0) {
105 LayerTreeImpl::~LayerTreeImpl() {
106 BreakSwapPromises(SwapPromise::SWAP_FAILS
);
108 // Need to explicitly clear the tree prior to destroying this so that
109 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
110 DCHECK(!root_layer_
);
111 DCHECK(layers_with_copy_output_request_
.empty());
114 void LayerTreeImpl::Shutdown() {
115 root_layer_
= nullptr;
118 void LayerTreeImpl::ReleaseResources() {
120 ReleaseResourcesRecursive(root_layer_
.get());
123 void LayerTreeImpl::SetRootLayer(scoped_ptr
<LayerImpl
> layer
) {
124 if (inner_viewport_scroll_layer_
)
125 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
126 if (outer_viewport_scroll_layer_
)
127 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
128 inner_viewport_scroll_delegate_proxy_
= nullptr;
129 outer_viewport_scroll_delegate_proxy_
= nullptr;
131 root_layer_
= layer
.Pass();
132 currently_scrolling_layer_
= NULL
;
133 inner_viewport_scroll_layer_
= NULL
;
134 outer_viewport_scroll_layer_
= NULL
;
135 page_scale_layer_
= NULL
;
137 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
140 LayerImpl
* LayerTreeImpl::InnerViewportScrollLayer() const {
141 return inner_viewport_scroll_layer_
;
144 LayerImpl
* LayerTreeImpl::OuterViewportScrollLayer() const {
145 return outer_viewport_scroll_layer_
;
148 gfx::ScrollOffset
LayerTreeImpl::TotalScrollOffset() const {
149 gfx::ScrollOffset offset
;
151 if (inner_viewport_scroll_layer_
)
152 offset
+= inner_viewport_scroll_layer_
->TotalScrollOffset();
154 if (outer_viewport_scroll_layer_
)
155 offset
+= outer_viewport_scroll_layer_
->TotalScrollOffset();
160 gfx::ScrollOffset
LayerTreeImpl::TotalMaxScrollOffset() const {
161 gfx::ScrollOffset offset
;
163 if (inner_viewport_scroll_layer_
)
164 offset
+= inner_viewport_scroll_layer_
->MaxScrollOffset();
166 if (outer_viewport_scroll_layer_
)
167 offset
+= outer_viewport_scroll_layer_
->MaxScrollOffset();
171 gfx::Vector2dF
LayerTreeImpl::TotalScrollDelta() const {
172 DCHECK(inner_viewport_scroll_layer_
);
173 gfx::Vector2dF delta
= inner_viewport_scroll_layer_
->ScrollDelta();
175 if (outer_viewport_scroll_layer_
)
176 delta
+= outer_viewport_scroll_layer_
->ScrollDelta();
181 scoped_ptr
<LayerImpl
> LayerTreeImpl::DetachLayerTree() {
182 // Clear all data structures that have direct references to the layer tree.
183 scrolling_layer_id_from_previous_tree_
=
184 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0;
185 if (inner_viewport_scroll_layer_
)
186 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
187 if (outer_viewport_scroll_layer_
)
188 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
189 inner_viewport_scroll_delegate_proxy_
= nullptr;
190 outer_viewport_scroll_delegate_proxy_
= nullptr;
191 inner_viewport_scroll_layer_
= NULL
;
192 outer_viewport_scroll_layer_
= NULL
;
193 page_scale_layer_
= NULL
;
194 currently_scrolling_layer_
= NULL
;
196 render_surface_layer_list_
.clear();
197 set_needs_update_draw_properties();
198 return root_layer_
.Pass();
201 void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl
* target_tree
) {
202 // The request queue should have been processed and does not require a push.
203 DCHECK_EQ(ui_resource_request_queue_
.size(), 0u);
205 if (next_activation_forces_redraw_
) {
206 target_tree
->ForceRedrawNextActivation();
207 next_activation_forces_redraw_
= false;
210 target_tree
->PassSwapPromises(&swap_promise_list_
);
212 target_tree
->top_controls_layout_height_
= top_controls_layout_height_
;
213 target_tree
->top_controls_content_offset_
= top_controls_content_offset_
;
214 target_tree
->top_controls_delta_
=
215 target_tree
->top_controls_delta_
-
216 target_tree
->sent_top_controls_delta_
;
217 target_tree
->sent_top_controls_delta_
= 0.f
;
219 target_tree
->SetPageScaleValues(
220 page_scale_factor(), min_page_scale_factor(), max_page_scale_factor(),
221 target_tree
->page_scale_delta() / target_tree
->sent_page_scale_delta());
222 target_tree
->set_sent_page_scale_delta(1);
224 target_tree
->page_scale_animation_
= page_scale_animation_
.Pass();
226 if (page_scale_layer_
&& inner_viewport_scroll_layer_
) {
227 target_tree
->SetViewportLayersFromIds(
228 page_scale_layer_
->id(),
229 inner_viewport_scroll_layer_
->id(),
230 outer_viewport_scroll_layer_
? outer_viewport_scroll_layer_
->id()
231 : Layer::INVALID_ID
);
233 target_tree
->ClearViewportLayers();
236 target_tree
->RegisterSelection(selection_start_
, selection_end_
);
238 // This should match the property synchronization in
239 // LayerTreeHost::finishCommitOnImplThread().
240 target_tree
->set_source_frame_number(source_frame_number());
241 target_tree
->set_background_color(background_color());
242 target_tree
->set_has_transparent_background(has_transparent_background());
244 if (ContentsTexturesPurged())
245 target_tree
->SetContentsTexturesPurged();
247 target_tree
->ResetContentsTexturesPurged();
249 if (ViewportSizeInvalid())
250 target_tree
->SetViewportSizeInvalid();
252 target_tree
->ResetViewportSizeInvalid();
255 target_tree
->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl
*>(
256 LayerTreeHostCommon::FindLayerInSubtree(
257 target_tree
->root_layer(), hud_layer()->id())));
259 target_tree
->set_hud_layer(NULL
);
261 target_tree
->has_ever_been_drawn_
= false;
264 LayerImpl
* LayerTreeImpl::InnerViewportContainerLayer() const {
265 return inner_viewport_scroll_layer_
266 ? inner_viewport_scroll_layer_
->scroll_clip_layer()
270 LayerImpl
* LayerTreeImpl::OuterViewportContainerLayer() const {
271 return outer_viewport_scroll_layer_
272 ? outer_viewport_scroll_layer_
->scroll_clip_layer()
276 LayerImpl
* LayerTreeImpl::CurrentlyScrollingLayer() const {
277 DCHECK(IsActiveTree());
278 return currently_scrolling_layer_
;
281 void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl
* layer
) {
282 if (currently_scrolling_layer_
== layer
)
285 if (currently_scrolling_layer_
&&
286 currently_scrolling_layer_
->scrollbar_animation_controller())
287 currently_scrolling_layer_
->scrollbar_animation_controller()
289 currently_scrolling_layer_
= layer
;
290 if (layer
&& layer
->scrollbar_animation_controller())
291 layer
->scrollbar_animation_controller()->DidScrollBegin();
294 void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
295 SetCurrentlyScrollingLayer(NULL
);
296 scrolling_layer_id_from_previous_tree_
= 0;
301 void ForceScrollbarParameterUpdateAfterScaleChange(LayerImpl
* current_layer
) {
305 while (current_layer
) {
306 current_layer
->ScrollbarParametersDidChange(false);
307 current_layer
= current_layer
->parent();
313 void LayerTreeImpl::SetPageScaleFactorAndLimits(float page_scale_factor
,
314 float min_page_scale_factor
, float max_page_scale_factor
) {
315 SetPageScaleValues(page_scale_factor
, min_page_scale_factor
,
316 max_page_scale_factor
, page_scale_delta_
);
319 void LayerTreeImpl::SetPageScaleDelta(float delta
) {
320 SetPageScaleValues(page_scale_factor_
, min_page_scale_factor_
,
321 max_page_scale_factor_
, delta
);
324 void LayerTreeImpl::SetPageScaleValues(float page_scale_factor
,
325 float min_page_scale_factor
, float max_page_scale_factor
,
326 float page_scale_delta
) {
327 bool page_scale_changed
=
328 min_page_scale_factor
!= min_page_scale_factor_
||
329 max_page_scale_factor
!= max_page_scale_factor_
||
330 page_scale_factor
!= page_scale_factor_
;
332 min_page_scale_factor_
= min_page_scale_factor
;
333 max_page_scale_factor_
= max_page_scale_factor
;
334 page_scale_factor_
= page_scale_factor
;
336 float total
= page_scale_factor_
* page_scale_delta
;
337 if (min_page_scale_factor_
&& total
< min_page_scale_factor_
)
338 page_scale_delta
= min_page_scale_factor_
/ page_scale_factor_
;
339 else if (max_page_scale_factor_
&& total
> max_page_scale_factor_
)
340 page_scale_delta
= max_page_scale_factor_
/ page_scale_factor_
;
342 if (page_scale_delta_
== page_scale_delta
&& !page_scale_changed
)
345 if (page_scale_delta_
!= page_scale_delta
) {
346 page_scale_delta_
= page_scale_delta
;
348 if (IsActiveTree()) {
349 LayerTreeImpl
* pending_tree
= layer_tree_host_impl_
->pending_tree();
351 DCHECK_EQ(1, pending_tree
->sent_page_scale_delta());
352 pending_tree
->SetPageScaleDelta(
353 page_scale_delta_
/ sent_page_scale_delta_
);
357 set_needs_update_draw_properties();
360 if (root_layer_scroll_offset_delegate_
) {
361 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
363 TotalMaxScrollOffset(),
365 total_page_scale_factor(),
366 min_page_scale_factor_
,
367 max_page_scale_factor_
);
370 ForceScrollbarParameterUpdateAfterScaleChange(page_scale_layer());
373 gfx::SizeF
LayerTreeImpl::ScrollableViewportSize() const {
374 if (!InnerViewportContainerLayer())
377 return gfx::ScaleSize(InnerViewportContainerLayer()->BoundsForScrolling(),
378 1.0f
/ total_page_scale_factor());
381 gfx::Rect
LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
382 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
383 ? OuterViewportScrollLayer()
384 : InnerViewportScrollLayer();
385 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
387 LayerImpl
* layer
= root_scroll_layer
->children()[0];
388 return MathUtil::MapEnclosingClippedRect(layer
->screen_space_transform(),
389 gfx::Rect(layer
->content_bounds()));
392 static void ApplySentScrollDeltasFromAbortedCommitTo(LayerImpl
* layer
) {
393 layer
->ApplySentScrollDeltasFromAbortedCommit();
396 void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
397 DCHECK(IsActiveTree());
399 page_scale_factor_
*= sent_page_scale_delta_
;
400 page_scale_delta_
/= sent_page_scale_delta_
;
401 sent_page_scale_delta_
= 1.f
;
403 top_controls_content_offset_
+= sent_top_controls_delta_
;
404 top_controls_delta_
-= sent_top_controls_delta_
;
405 sent_top_controls_delta_
= 0.f
;
410 LayerTreeHostCommon::CallFunctionForSubtree(
411 root_layer(), base::Bind(&ApplySentScrollDeltasFromAbortedCommitTo
));
414 static void ApplyScrollDeltasSinceBeginMainFrameTo(LayerImpl
* layer
) {
415 layer
->ApplyScrollDeltasSinceBeginMainFrame();
418 void LayerTreeImpl::ApplyScrollDeltasSinceBeginMainFrame() {
419 DCHECK(IsPendingTree());
423 LayerTreeHostCommon::CallFunctionForSubtree(
424 root_layer(), base::Bind(&ApplyScrollDeltasSinceBeginMainFrameTo
));
427 void LayerTreeImpl::SetViewportLayersFromIds(
428 int page_scale_layer_id
,
429 int inner_viewport_scroll_layer_id
,
430 int outer_viewport_scroll_layer_id
) {
431 page_scale_layer_
= LayerById(page_scale_layer_id
);
432 DCHECK(page_scale_layer_
);
434 inner_viewport_scroll_layer_
=
435 LayerById(inner_viewport_scroll_layer_id
);
436 DCHECK(inner_viewport_scroll_layer_
);
438 outer_viewport_scroll_layer_
=
439 LayerById(outer_viewport_scroll_layer_id
);
440 DCHECK(outer_viewport_scroll_layer_
||
441 outer_viewport_scroll_layer_id
== Layer::INVALID_ID
);
443 if (!root_layer_scroll_offset_delegate_
)
446 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
447 new LayerScrollOffsetDelegateProxy(inner_viewport_scroll_layer_
,
448 root_layer_scroll_offset_delegate_
,
451 if (outer_viewport_scroll_layer_
)
452 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
453 new LayerScrollOffsetDelegateProxy(outer_viewport_scroll_layer_
,
454 root_layer_scroll_offset_delegate_
,
458 void LayerTreeImpl::ClearViewportLayers() {
459 page_scale_layer_
= NULL
;
460 inner_viewport_scroll_layer_
= NULL
;
461 outer_viewport_scroll_layer_
= NULL
;
464 bool LayerTreeImpl::UpdateDrawProperties() {
465 if (!needs_update_draw_properties_
)
468 // For max_texture_size.
469 if (!layer_tree_host_impl_
->renderer())
475 needs_update_draw_properties_
= false;
476 render_surface_layer_list_
.clear();
480 "LayerTreeImpl::UpdateDrawProperties",
484 source_frame_number_
);
485 LayerImpl
* page_scale_layer
=
486 page_scale_layer_
? page_scale_layer_
: InnerViewportContainerLayer();
487 bool can_render_to_separate_surface
=
488 (layer_tree_host_impl_
->GetDrawMode() !=
489 DRAW_MODE_RESOURCELESS_SOFTWARE
);
491 ++render_surface_layer_list_id_
;
492 LayerTreeHostCommon::CalcDrawPropsImplInputs
inputs(
495 layer_tree_host_impl_
->DrawTransform(),
496 device_scale_factor(),
497 total_page_scale_factor(),
499 resource_provider()->max_texture_size(),
500 settings().can_use_lcd_text
,
501 can_render_to_separate_surface
,
502 settings().layer_transforms_should_scale_layer_contents
,
503 &render_surface_layer_list_
,
504 render_surface_layer_list_id_
);
505 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
509 TRACE_EVENT_BEGIN2("cc", "LayerTreeImpl::UpdateTilePriorities", "IsActive",
510 IsActiveTree(), "SourceFrameNumber",
511 source_frame_number_
);
512 scoped_ptr
<OcclusionTracker
<LayerImpl
>> occlusion_tracker
;
513 if (settings().use_occlusion_for_tile_prioritization
) {
514 occlusion_tracker
.reset(new OcclusionTracker
<LayerImpl
>(
515 root_layer()->render_surface()->content_rect()));
516 occlusion_tracker
->set_minimum_tracking_size(
517 settings().minimum_occlusion_tracking_size
);
520 bool resourceless_software_draw
= (layer_tree_host_impl_
->GetDrawMode() ==
521 DRAW_MODE_RESOURCELESS_SOFTWARE
);
523 // LayerIterator is used here instead of CallFunctionForSubtree to only
524 // UpdateTilePriorities on layers that will be visible (and thus have valid
525 // draw properties) and not because any ordering is required.
526 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
527 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
528 size_t layers_updated_count
= 0;
529 for (LayerIteratorType it
=
530 LayerIteratorType::Begin(&render_surface_layer_list_
);
533 if (occlusion_tracker
)
534 occlusion_tracker
->EnterLayer(it
);
536 LayerImpl
* layer
= *it
;
537 const Occlusion
& occlusion_in_content_space
=
538 occlusion_tracker
? occlusion_tracker
->GetCurrentOcclusionForLayer(
539 layer
->draw_transform())
542 if (it
.represents_itself()) {
543 layer
->UpdateTiles(occlusion_in_content_space
,
544 resourceless_software_draw
);
545 ++layers_updated_count
;
548 if (!it
.represents_contributing_render_surface()) {
549 if (occlusion_tracker
)
550 occlusion_tracker
->LeaveLayer(it
);
554 if (layer
->mask_layer()) {
555 layer
->mask_layer()->UpdateTiles(occlusion_in_content_space
,
556 resourceless_software_draw
);
557 ++layers_updated_count
;
559 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
560 layer
->replica_layer()->mask_layer()->UpdateTiles(
561 occlusion_in_content_space
, resourceless_software_draw
);
562 ++layers_updated_count
;
565 if (occlusion_tracker
)
566 occlusion_tracker
->LeaveLayer(it
);
569 TRACE_EVENT_END1("cc", "LayerTreeImpl::UpdateTilePriorities",
570 "layers_updated_count", layers_updated_count
);
573 DCHECK(!needs_update_draw_properties_
) <<
574 "CalcDrawProperties should not set_needs_update_draw_properties()";
578 const LayerImplList
& LayerTreeImpl::RenderSurfaceLayerList() const {
579 // If this assert triggers, then the list is dirty.
580 DCHECK(!needs_update_draw_properties_
);
581 return render_surface_layer_list_
;
584 gfx::Size
LayerTreeImpl::ScrollableSize() const {
585 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
586 ? OuterViewportScrollLayer()
587 : InnerViewportScrollLayer();
588 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
590 return root_scroll_layer
->children()[0]->bounds();
593 LayerImpl
* LayerTreeImpl::LayerById(int id
) {
594 LayerIdMap::iterator iter
= layer_id_map_
.find(id
);
595 return iter
!= layer_id_map_
.end() ? iter
->second
: NULL
;
598 void LayerTreeImpl::RegisterLayer(LayerImpl
* layer
) {
599 DCHECK(!LayerById(layer
->id()));
600 layer_id_map_
[layer
->id()] = layer
;
603 void LayerTreeImpl::UnregisterLayer(LayerImpl
* layer
) {
604 DCHECK(LayerById(layer
->id()));
605 layer_id_map_
.erase(layer
->id());
608 size_t LayerTreeImpl::NumLayers() {
609 return layer_id_map_
.size();
612 void LayerTreeImpl::PushPersistedState(LayerTreeImpl
* pending_tree
) {
613 pending_tree
->SetCurrentlyScrollingLayer(
614 LayerTreeHostCommon::FindLayerInSubtree(pending_tree
->root_layer(),
615 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0));
618 static void DidBecomeActiveRecursive(LayerImpl
* layer
) {
619 layer
->DidBecomeActive();
620 if (layer
->mask_layer())
621 layer
->mask_layer()->DidBecomeActive();
622 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer())
623 layer
->replica_layer()->mask_layer()->DidBecomeActive();
625 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
626 DidBecomeActiveRecursive(layer
->children()[i
]);
629 void LayerTreeImpl::DidBecomeActive() {
630 if (next_activation_forces_redraw_
) {
631 layer_tree_host_impl_
->SetFullRootLayerDamage();
632 next_activation_forces_redraw_
= false;
635 if (scrolling_layer_id_from_previous_tree_
) {
636 currently_scrolling_layer_
= LayerTreeHostCommon::FindLayerInSubtree(
637 root_layer(), scrolling_layer_id_from_previous_tree_
);
640 // Always reset this flag on activation, as we would only have activated
641 // if we were in a good state.
642 layer_tree_host_impl_
->ResetRequiresHighResToDraw();
645 DidBecomeActiveRecursive(root_layer());
647 devtools_instrumentation::DidActivateLayerTree(layer_tree_host_impl_
->id(),
648 source_frame_number_
);
651 bool LayerTreeImpl::ContentsTexturesPurged() const {
652 return contents_textures_purged_
;
655 void LayerTreeImpl::SetContentsTexturesPurged() {
656 if (contents_textures_purged_
)
658 contents_textures_purged_
= true;
659 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
662 void LayerTreeImpl::ResetContentsTexturesPurged() {
663 if (!contents_textures_purged_
)
665 contents_textures_purged_
= false;
666 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
669 bool LayerTreeImpl::RequiresHighResToDraw() const {
670 return layer_tree_host_impl_
->RequiresHighResToDraw();
673 bool LayerTreeImpl::ViewportSizeInvalid() const {
674 return viewport_size_invalid_
;
677 void LayerTreeImpl::SetViewportSizeInvalid() {
678 viewport_size_invalid_
= true;
679 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
682 void LayerTreeImpl::ResetViewportSizeInvalid() {
683 viewport_size_invalid_
= false;
684 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
687 Proxy
* LayerTreeImpl::proxy() const {
688 return layer_tree_host_impl_
->proxy();
691 const LayerTreeSettings
& LayerTreeImpl::settings() const {
692 return layer_tree_host_impl_
->settings();
695 const RendererCapabilitiesImpl
& LayerTreeImpl::GetRendererCapabilities() const {
696 return layer_tree_host_impl_
->GetRendererCapabilities();
699 ContextProvider
* LayerTreeImpl::context_provider() const {
700 return output_surface()->context_provider();
703 OutputSurface
* LayerTreeImpl::output_surface() const {
704 return layer_tree_host_impl_
->output_surface();
707 ResourceProvider
* LayerTreeImpl::resource_provider() const {
708 return layer_tree_host_impl_
->resource_provider();
711 TileManager
* LayerTreeImpl::tile_manager() const {
712 return layer_tree_host_impl_
->tile_manager();
715 FrameRateCounter
* LayerTreeImpl::frame_rate_counter() const {
716 return layer_tree_host_impl_
->fps_counter();
719 PaintTimeCounter
* LayerTreeImpl::paint_time_counter() const {
720 return layer_tree_host_impl_
->paint_time_counter();
723 MemoryHistory
* LayerTreeImpl::memory_history() const {
724 return layer_tree_host_impl_
->memory_history();
727 gfx::Size
LayerTreeImpl::device_viewport_size() const {
728 return layer_tree_host_impl_
->device_viewport_size();
731 bool LayerTreeImpl::IsActiveTree() const {
732 return layer_tree_host_impl_
->active_tree() == this;
735 bool LayerTreeImpl::IsPendingTree() const {
736 return layer_tree_host_impl_
->pending_tree() == this;
739 bool LayerTreeImpl::IsRecycleTree() const {
740 return layer_tree_host_impl_
->recycle_tree() == this;
743 LayerImpl
* LayerTreeImpl::FindActiveTreeLayerById(int id
) {
744 LayerTreeImpl
* tree
= layer_tree_host_impl_
->active_tree();
747 return tree
->LayerById(id
);
750 LayerImpl
* LayerTreeImpl::FindPendingTreeLayerById(int id
) {
751 LayerTreeImpl
* tree
= layer_tree_host_impl_
->pending_tree();
754 return tree
->LayerById(id
);
757 bool LayerTreeImpl::PinchGestureActive() const {
758 return layer_tree_host_impl_
->pinch_gesture_active();
761 BeginFrameArgs
LayerTreeImpl::CurrentBeginFrameArgs() const {
762 return layer_tree_host_impl_
->CurrentBeginFrameArgs();
765 base::TimeDelta
LayerTreeImpl::begin_impl_frame_interval() const {
766 return layer_tree_host_impl_
->begin_impl_frame_interval();
769 void LayerTreeImpl::SetNeedsCommit() {
770 layer_tree_host_impl_
->SetNeedsCommit();
773 gfx::Rect
LayerTreeImpl::DeviceViewport() const {
774 return layer_tree_host_impl_
->DeviceViewport();
777 gfx::Size
LayerTreeImpl::DrawViewportSize() const {
778 return layer_tree_host_impl_
->DrawViewportSize();
781 const gfx::Rect
LayerTreeImpl::ViewportRectForTilePriority() const {
782 return layer_tree_host_impl_
->ViewportRectForTilePriority();
785 scoped_ptr
<ScrollbarAnimationController
>
786 LayerTreeImpl::CreateScrollbarAnimationController(LayerImpl
* scrolling_layer
) {
787 DCHECK(settings().scrollbar_fade_delay_ms
);
788 DCHECK(settings().scrollbar_fade_duration_ms
);
789 base::TimeDelta delay
=
790 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_delay_ms
);
791 base::TimeDelta resize_delay
= base::TimeDelta::FromMilliseconds(
792 settings().scrollbar_fade_resize_delay_ms
);
793 base::TimeDelta duration
=
794 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_duration_ms
);
795 switch (settings().scrollbar_animator
) {
796 case LayerTreeSettings::LinearFade
: {
797 return ScrollbarAnimationControllerLinearFade::Create(
799 layer_tree_host_impl_
,
804 case LayerTreeSettings::Thinning
: {
805 return ScrollbarAnimationControllerThinning::Create(scrolling_layer
,
806 layer_tree_host_impl_
,
811 case LayerTreeSettings::NoAnimator
:
818 void LayerTreeImpl::DidAnimateScrollOffset() {
819 layer_tree_host_impl_
->DidAnimateScrollOffset();
822 bool LayerTreeImpl::use_gpu_rasterization() const {
823 return layer_tree_host_impl_
->use_gpu_rasterization();
826 bool LayerTreeImpl::create_low_res_tiling() const {
827 return layer_tree_host_impl_
->create_low_res_tiling();
830 void LayerTreeImpl::SetNeedsRedraw() {
831 layer_tree_host_impl_
->SetNeedsRedraw();
834 const LayerTreeDebugState
& LayerTreeImpl::debug_state() const {
835 return layer_tree_host_impl_
->debug_state();
838 float LayerTreeImpl::device_scale_factor() const {
839 return layer_tree_host_impl_
->device_scale_factor();
842 DebugRectHistory
* LayerTreeImpl::debug_rect_history() const {
843 return layer_tree_host_impl_
->debug_rect_history();
846 AnimationRegistrar
* LayerTreeImpl::animationRegistrar() const {
847 return layer_tree_host_impl_
->animation_registrar();
850 void LayerTreeImpl::GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const {
851 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
852 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
853 for (LayerIteratorType it
=
854 LayerIteratorType::Begin(&render_surface_layer_list_
);
857 if (!it
.represents_itself())
859 LayerImpl
* layer_impl
= *it
;
860 layer_impl
->GetAllTilesForTracing(tiles
);
864 void LayerTreeImpl::AsValueInto(base::debug::TracedValue
* state
) const {
865 TracedValue::MakeDictIntoImplicitSnapshot(state
, "cc::LayerTreeImpl", this);
866 state
->SetInteger("source_frame_number", source_frame_number_
);
868 state
->BeginDictionary("root_layer");
869 root_layer_
->AsValueInto(state
);
870 state
->EndDictionary();
872 state
->BeginArray("render_surface_layer_list");
873 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
874 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
875 for (LayerIteratorType it
= LayerIteratorType::Begin(
876 &render_surface_layer_list_
); it
!= end
; ++it
) {
877 if (!it
.represents_itself())
879 TracedValue::AppendIDRef(*it
, state
);
883 state
->BeginArray("swap_promise_trace_ids");
884 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
885 state
->AppendDouble(swap_promise_list_
[i
]->TraceId());
889 void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
890 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
) {
891 if (root_layer_scroll_offset_delegate_
== root_layer_scroll_offset_delegate
)
894 if (!root_layer_scroll_offset_delegate
) {
895 // Make sure we remove the proxies from their layers before
897 if (InnerViewportScrollLayer())
898 InnerViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
899 if (OuterViewportScrollLayer())
900 OuterViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
901 inner_viewport_scroll_delegate_proxy_
= nullptr;
902 outer_viewport_scroll_delegate_proxy_
= nullptr;
905 root_layer_scroll_offset_delegate_
= root_layer_scroll_offset_delegate
;
907 if (root_layer_scroll_offset_delegate_
) {
908 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
910 TotalMaxScrollOffset(),
912 total_page_scale_factor(),
913 min_page_scale_factor(),
914 max_page_scale_factor());
916 if (inner_viewport_scroll_layer_
) {
917 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
918 new LayerScrollOffsetDelegateProxy(InnerViewportScrollLayer(),
919 root_layer_scroll_offset_delegate_
,
921 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(
922 inner_viewport_scroll_delegate_proxy_
.get());
925 if (outer_viewport_scroll_layer_
) {
926 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
927 new LayerScrollOffsetDelegateProxy(OuterViewportScrollLayer(),
928 root_layer_scroll_offset_delegate_
,
930 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(
931 outer_viewport_scroll_delegate_proxy_
.get());
934 if (inner_viewport_scroll_layer_
)
935 UpdateScrollOffsetDelegate();
939 void LayerTreeImpl::OnRootLayerDelegatedScrollOffsetChanged() {
940 DCHECK(root_layer_scroll_offset_delegate_
);
941 if (inner_viewport_scroll_layer_
) {
942 inner_viewport_scroll_layer_
->DidScroll();
944 if (outer_viewport_scroll_layer_
) {
945 outer_viewport_scroll_layer_
->DidScroll();
949 void LayerTreeImpl::UpdateScrollOffsetDelegate() {
950 DCHECK(InnerViewportScrollLayer());
951 DCHECK(!OuterViewportScrollLayer() || outer_viewport_scroll_delegate_proxy_
);
952 DCHECK(root_layer_scroll_offset_delegate_
);
954 gfx::ScrollOffset offset
=
955 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
957 if (OuterViewportScrollLayer())
958 offset
+= outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
960 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
962 TotalMaxScrollOffset(),
964 total_page_scale_factor(),
965 min_page_scale_factor(),
966 max_page_scale_factor());
969 gfx::ScrollOffset
LayerTreeImpl::GetDelegatedScrollOffset(LayerImpl
* layer
) {
970 DCHECK(root_layer_scroll_offset_delegate_
);
971 DCHECK(InnerViewportScrollLayer());
972 if (layer
== InnerViewportScrollLayer() && !OuterViewportScrollLayer())
973 return root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
975 // If we get here, we have both inner/outer viewports, and need to distribute
976 // the scroll offset between them.
977 DCHECK(inner_viewport_scroll_delegate_proxy_
);
978 DCHECK(outer_viewport_scroll_delegate_proxy_
);
979 gfx::ScrollOffset inner_viewport_offset
=
980 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
981 gfx::ScrollOffset outer_viewport_offset
=
982 outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
984 // It may be nothing has changed.
985 gfx::ScrollOffset delegate_offset
=
986 root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
987 if (inner_viewport_offset
+ outer_viewport_offset
== delegate_offset
) {
988 if (layer
== InnerViewportScrollLayer())
989 return inner_viewport_offset
;
991 return outer_viewport_offset
;
994 gfx::ScrollOffset max_outer_viewport_scroll_offset
=
995 OuterViewportScrollLayer()->MaxScrollOffset();
997 outer_viewport_offset
= delegate_offset
- inner_viewport_offset
;
998 outer_viewport_offset
.SetToMin(max_outer_viewport_scroll_offset
);
999 outer_viewport_offset
.SetToMax(gfx::ScrollOffset());
1001 if (layer
== OuterViewportScrollLayer())
1002 return outer_viewport_offset
;
1004 inner_viewport_offset
= delegate_offset
- outer_viewport_offset
;
1006 return inner_viewport_offset
;
1009 void LayerTreeImpl::QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
) {
1010 DCHECK(swap_promise
);
1011 swap_promise_list_
.push_back(swap_promise
.Pass());
1014 void LayerTreeImpl::PassSwapPromises(
1015 ScopedPtrVector
<SwapPromise
>* new_swap_promise
) {
1016 swap_promise_list_
.insert_and_take(swap_promise_list_
.end(),
1018 new_swap_promise
->clear();
1021 void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata
* metadata
) {
1022 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
1023 swap_promise_list_
[i
]->DidSwap(metadata
);
1024 swap_promise_list_
.clear();
1027 void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason
) {
1028 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
1029 swap_promise_list_
[i
]->DidNotSwap(reason
);
1030 swap_promise_list_
.clear();
1033 void LayerTreeImpl::DidModifyTilePriorities() {
1034 layer_tree_host_impl_
->DidModifyTilePriorities();
1037 void LayerTreeImpl::set_ui_resource_request_queue(
1038 const UIResourceRequestQueue
& queue
) {
1039 ui_resource_request_queue_
= queue
;
1042 ResourceProvider::ResourceId
LayerTreeImpl::ResourceIdForUIResource(
1043 UIResourceId uid
) const {
1044 return layer_tree_host_impl_
->ResourceIdForUIResource(uid
);
1047 bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid
) const {
1048 return layer_tree_host_impl_
->IsUIResourceOpaque(uid
);
1051 void LayerTreeImpl::ProcessUIResourceRequestQueue() {
1052 while (ui_resource_request_queue_
.size() > 0) {
1053 UIResourceRequest req
= ui_resource_request_queue_
.front();
1054 ui_resource_request_queue_
.pop_front();
1056 switch (req
.GetType()) {
1057 case UIResourceRequest::UIResourceCreate
:
1058 layer_tree_host_impl_
->CreateUIResource(req
.GetId(), req
.GetBitmap());
1060 case UIResourceRequest::UIResourceDelete
:
1061 layer_tree_host_impl_
->DeleteUIResource(req
.GetId());
1063 case UIResourceRequest::UIResourceInvalidRequest
:
1069 // If all UI resource evictions were not recreated by processing this queue,
1070 // then another commit is required.
1071 if (layer_tree_host_impl_
->EvictedUIResourcesExist())
1072 layer_tree_host_impl_
->SetNeedsCommit();
1075 void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl
* layer
) {
1076 // Only the active tree needs to know about layers with copy requests, as
1077 // they are aborted if not serviced during draw.
1078 DCHECK(IsActiveTree());
1080 // DCHECK(std::find(layers_with_copy_output_request_.begin(),
1081 // layers_with_copy_output_request_.end(),
1082 // layer) == layers_with_copy_output_request_.end());
1083 // TODO(danakj): Remove this once crash is found crbug.com/309777
1084 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
1085 CHECK(layers_with_copy_output_request_
[i
] != layer
)
1086 << i
<< " of " << layers_with_copy_output_request_
.size();
1088 layers_with_copy_output_request_
.push_back(layer
);
1091 void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
) {
1092 // Only the active tree needs to know about layers with copy requests, as
1093 // they are aborted if not serviced during draw.
1094 DCHECK(IsActiveTree());
1096 std::vector
<LayerImpl
*>::iterator it
= std::find(
1097 layers_with_copy_output_request_
.begin(),
1098 layers_with_copy_output_request_
.end(),
1100 DCHECK(it
!= layers_with_copy_output_request_
.end());
1101 layers_with_copy_output_request_
.erase(it
);
1103 // TODO(danakj): Remove this once crash is found crbug.com/309777
1104 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
1105 CHECK(layers_with_copy_output_request_
[i
] != layer
)
1106 << i
<< " of " << layers_with_copy_output_request_
.size();
1110 const std::vector
<LayerImpl
*>& LayerTreeImpl::LayersWithCopyOutputRequest()
1112 // Only the active tree needs to know about layers with copy requests, as
1113 // they are aborted if not serviced during draw.
1114 DCHECK(IsActiveTree());
1116 return layers_with_copy_output_request_
;
1119 void LayerTreeImpl::ReleaseResourcesRecursive(LayerImpl
* current
) {
1121 current
->ReleaseResources();
1122 if (current
->mask_layer())
1123 ReleaseResourcesRecursive(current
->mask_layer());
1124 if (current
->replica_layer())
1125 ReleaseResourcesRecursive(current
->replica_layer());
1126 for (size_t i
= 0; i
< current
->children().size(); ++i
)
1127 ReleaseResourcesRecursive(current
->children()[i
]);
1130 template <typename LayerType
>
1131 static inline bool LayerClipsSubtree(LayerType
* layer
) {
1132 return layer
->masks_to_bounds() || layer
->mask_layer();
1135 static bool PointHitsRect(
1136 const gfx::PointF
& screen_space_point
,
1137 const gfx::Transform
& local_space_to_screen_space_transform
,
1138 const gfx::RectF
& local_space_rect
,
1139 float* distance_to_camera
) {
1140 // If the transform is not invertible, then assume that this point doesn't hit
1142 gfx::Transform
inverse_local_space_to_screen_space(
1143 gfx::Transform::kSkipInitialization
);
1144 if (!local_space_to_screen_space_transform
.GetInverse(
1145 &inverse_local_space_to_screen_space
))
1148 // Transform the hit test point from screen space to the local space of the
1150 bool clipped
= false;
1151 gfx::Point3F planar_point
= MathUtil::ProjectPoint3D(
1152 inverse_local_space_to_screen_space
, screen_space_point
, &clipped
);
1153 gfx::PointF hit_test_point_in_local_space
=
1154 gfx::PointF(planar_point
.x(), planar_point
.y());
1156 // If ProjectPoint could not project to a valid value, then we assume that
1157 // this point doesn't hit this rect.
1161 if (!local_space_rect
.Contains(hit_test_point_in_local_space
))
1164 if (distance_to_camera
) {
1165 // To compute the distance to the camera, we have to take the planar point
1166 // and pull it back to world space and compute the displacement along the
1168 gfx::Point3F
planar_point_in_screen_space(planar_point
);
1169 local_space_to_screen_space_transform
.TransformPoint(
1170 &planar_point_in_screen_space
);
1171 *distance_to_camera
= planar_point_in_screen_space
.z();
1177 static bool PointHitsRegion(const gfx::PointF
& screen_space_point
,
1178 const gfx::Transform
& screen_space_transform
,
1179 const Region
& layer_space_region
,
1180 float layer_content_scale_x
,
1181 float layer_content_scale_y
) {
1182 // If the transform is not invertible, then assume that this point doesn't hit
1184 gfx::Transform
inverse_screen_space_transform(
1185 gfx::Transform::kSkipInitialization
);
1186 if (!screen_space_transform
.GetInverse(&inverse_screen_space_transform
))
1189 // Transform the hit test point from screen space to the local space of the
1191 bool clipped
= false;
1192 gfx::PointF hit_test_point_in_content_space
= MathUtil::ProjectPoint(
1193 inverse_screen_space_transform
, screen_space_point
, &clipped
);
1194 gfx::PointF hit_test_point_in_layer_space
=
1195 gfx::ScalePoint(hit_test_point_in_content_space
,
1196 1.f
/ layer_content_scale_x
,
1197 1.f
/ layer_content_scale_y
);
1199 // If ProjectPoint could not project to a valid value, then we assume that
1200 // this point doesn't hit this region.
1204 return layer_space_region
.Contains(
1205 gfx::ToRoundedPoint(hit_test_point_in_layer_space
));
1208 static const LayerImpl
* GetNextClippingLayer(const LayerImpl
* layer
) {
1209 if (layer
->scroll_parent())
1210 return layer
->scroll_parent();
1211 if (layer
->clip_parent())
1212 return layer
->clip_parent();
1213 return layer
->parent();
1216 static bool PointIsClippedBySurfaceOrClipRect(
1217 const gfx::PointF
& screen_space_point
,
1218 const LayerImpl
* layer
) {
1219 // Walk up the layer tree and hit-test any render_surfaces and any layer
1220 // clip rects that are active.
1221 for (; layer
; layer
= GetNextClippingLayer(layer
)) {
1222 if (layer
->render_surface() &&
1223 !PointHitsRect(screen_space_point
,
1224 layer
->render_surface()->screen_space_transform(),
1225 layer
->render_surface()->content_rect(),
1229 if (LayerClipsSubtree(layer
) &&
1230 !PointHitsRect(screen_space_point
,
1231 layer
->screen_space_transform(),
1232 gfx::Rect(layer
->content_bounds()),
1237 // If we have finished walking all ancestors without having already exited,
1238 // then the point is not clipped by any ancestors.
1242 static bool PointHitsLayer(const LayerImpl
* layer
,
1243 const gfx::PointF
& screen_space_point
,
1244 float* distance_to_intersection
) {
1245 gfx::RectF
content_rect(layer
->content_bounds());
1246 if (!PointHitsRect(screen_space_point
,
1247 layer
->screen_space_transform(),
1249 distance_to_intersection
))
1252 // At this point, we think the point does hit the layer, but we need to walk
1253 // up the parents to ensure that the layer was not clipped in such a way
1254 // that the hit point actually should not hit the layer.
1255 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer
))
1258 // Skip the HUD layer.
1259 if (layer
== layer
->layer_tree_impl()->hud_layer())
1265 struct FindClosestMatchingLayerDataForRecursion
{
1266 FindClosestMatchingLayerDataForRecursion()
1267 : closest_match(NULL
),
1268 closest_distance(-std::numeric_limits
<float>::infinity()) {}
1269 LayerImpl
* closest_match
;
1270 // Note that the positive z-axis points towards the camera, so bigger means
1271 // closer in this case, counterintuitively.
1272 float closest_distance
;
1275 template <typename Functor
>
1276 static void FindClosestMatchingLayer(
1277 const gfx::PointF
& screen_space_point
,
1279 const Functor
& func
,
1280 FindClosestMatchingLayerDataForRecursion
* data_for_recursion
) {
1281 for (int i
= layer
->children().size() - 1; i
>= 0; --i
) {
1282 FindClosestMatchingLayer(
1283 screen_space_point
, layer
->children()[i
], func
, data_for_recursion
);
1286 float distance_to_intersection
= 0.f
;
1288 PointHitsLayer(layer
, screen_space_point
, &distance_to_intersection
) &&
1289 ((!data_for_recursion
->closest_match
||
1290 distance_to_intersection
> data_for_recursion
->closest_distance
))) {
1291 data_for_recursion
->closest_distance
= distance_to_intersection
;
1292 data_for_recursion
->closest_match
= layer
;
1296 static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl
* layer
) {
1297 if (!layer
->scrollable())
1299 if (layer
->IsDrawnRenderSurfaceLayerListMember())
1301 if (!layer
->scroll_children())
1303 for (std::set
<LayerImpl
*>::const_iterator it
=
1304 layer
->scroll_children()->begin();
1305 it
!= layer
->scroll_children()->end();
1307 if ((*it
)->IsDrawnRenderSurfaceLayerListMember())
1313 struct FindScrollingLayerFunctor
{
1314 bool operator()(LayerImpl
* layer
) const {
1315 return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
);
1319 LayerImpl
* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint(
1320 const gfx::PointF
& screen_space_point
) {
1321 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1322 FindClosestMatchingLayer(screen_space_point
,
1324 FindScrollingLayerFunctor(),
1325 &data_for_recursion
);
1326 return data_for_recursion
.closest_match
;
1329 struct HitTestVisibleScrollableOrTouchableFunctor
{
1330 bool operator()(LayerImpl
* layer
) const {
1331 return layer
->IsDrawnRenderSurfaceLayerListMember() ||
1332 ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
) ||
1333 !layer
->touch_event_handler_region().IsEmpty() ||
1334 layer
->have_wheel_event_handlers();
1338 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPoint(
1339 const gfx::PointF
& screen_space_point
) {
1342 if (!UpdateDrawProperties())
1344 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1345 FindClosestMatchingLayer(screen_space_point
,
1347 HitTestVisibleScrollableOrTouchableFunctor(),
1348 &data_for_recursion
);
1349 return data_for_recursion
.closest_match
;
1352 static bool LayerHasTouchEventHandlersAt(const gfx::PointF
& screen_space_point
,
1353 LayerImpl
* layer_impl
) {
1354 if (layer_impl
->touch_event_handler_region().IsEmpty())
1357 if (!PointHitsRegion(screen_space_point
,
1358 layer_impl
->screen_space_transform(),
1359 layer_impl
->touch_event_handler_region(),
1360 layer_impl
->contents_scale_x(),
1361 layer_impl
->contents_scale_y()))
1364 // At this point, we think the point does hit the touch event handler region
1365 // on the layer, but we need to walk up the parents to ensure that the layer
1366 // was not clipped in such a way that the hit point actually should not hit
1368 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer_impl
))
1374 struct FindTouchEventLayerFunctor
{
1375 bool operator()(LayerImpl
* layer
) const {
1376 return LayerHasTouchEventHandlersAt(screen_space_point
, layer
);
1378 const gfx::PointF screen_space_point
;
1381 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
1382 const gfx::PointF
& screen_space_point
) {
1385 if (!UpdateDrawProperties())
1387 FindTouchEventLayerFunctor func
= {screen_space_point
};
1388 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1389 FindClosestMatchingLayer(
1390 screen_space_point
, root_layer(), func
, &data_for_recursion
);
1391 return data_for_recursion
.closest_match
;
1394 void LayerTreeImpl::RegisterSelection(const LayerSelectionBound
& start
,
1395 const LayerSelectionBound
& end
) {
1396 selection_start_
= start
;
1397 selection_end_
= end
;
1400 static ViewportSelectionBound
ComputeViewportSelection(
1401 const LayerSelectionBound
& layer_bound
,
1403 float device_scale_factor
) {
1404 ViewportSelectionBound viewport_bound
;
1405 viewport_bound
.type
= layer_bound
.type
;
1407 if (!layer
|| layer_bound
.type
== SELECTION_BOUND_EMPTY
)
1408 return viewport_bound
;
1410 gfx::PointF layer_scaled_top
= gfx::ScalePoint(layer_bound
.edge_top
,
1411 layer
->contents_scale_x(),
1412 layer
->contents_scale_y());
1413 gfx::PointF layer_scaled_bottom
= gfx::ScalePoint(layer_bound
.edge_bottom
,
1414 layer
->contents_scale_x(),
1415 layer
->contents_scale_y());
1417 bool clipped
= false;
1418 gfx::PointF screen_top
= MathUtil::MapPoint(
1419 layer
->screen_space_transform(), layer_scaled_top
, &clipped
);
1420 gfx::PointF screen_bottom
= MathUtil::MapPoint(
1421 layer
->screen_space_transform(), layer_scaled_bottom
, &clipped
);
1423 const float inv_scale
= 1.f
/ device_scale_factor
;
1424 viewport_bound
.edge_top
= gfx::ScalePoint(screen_top
, inv_scale
);
1425 viewport_bound
.edge_bottom
= gfx::ScalePoint(screen_bottom
, inv_scale
);
1427 // The bottom edge point is used for visibility testing as it is the logical
1428 // focal point for bound selection handles (this may change in the future).
1429 // Shifting the visibility point fractionally inward ensures that neighboring
1430 // or logically coincident layers aligned to integral DPI coordinates will not
1431 // spuriously occlude the bound.
1432 gfx::Vector2dF visibility_offset
= layer_scaled_top
- layer_scaled_bottom
;
1433 visibility_offset
.Scale(device_scale_factor
/ visibility_offset
.Length());
1434 gfx::PointF visibility_point
= layer_scaled_bottom
+ visibility_offset
;
1435 if (visibility_point
.x() <= 0)
1436 visibility_point
.set_x(visibility_point
.x() + device_scale_factor
);
1437 visibility_point
= MathUtil::MapPoint(
1438 layer
->screen_space_transform(), visibility_point
, &clipped
);
1440 float intersect_distance
= 0.f
;
1441 viewport_bound
.visible
=
1442 PointHitsLayer(layer
, visibility_point
, &intersect_distance
);
1444 return viewport_bound
;
1447 void LayerTreeImpl::GetViewportSelection(ViewportSelectionBound
* start
,
1448 ViewportSelectionBound
* end
) {
1452 *start
= ComputeViewportSelection(
1454 selection_start_
.layer_id
? LayerById(selection_start_
.layer_id
) : NULL
,
1455 device_scale_factor());
1456 if (start
->type
== SELECTION_BOUND_CENTER
||
1457 start
->type
== SELECTION_BOUND_EMPTY
) {
1460 *end
= ComputeViewportSelection(
1462 selection_end_
.layer_id
? LayerById(selection_end_
.layer_id
) : NULL
,
1463 device_scale_factor());
1467 void LayerTreeImpl::RegisterPictureLayerImpl(PictureLayerImpl
* layer
) {
1468 layer_tree_host_impl_
->RegisterPictureLayerImpl(layer
);
1471 void LayerTreeImpl::UnregisterPictureLayerImpl(PictureLayerImpl
* layer
) {
1472 layer_tree_host_impl_
->UnregisterPictureLayerImpl(layer
);
1475 void LayerTreeImpl::InputScrollAnimationFinished() {
1476 layer_tree_host_impl_
->ScrollEnd();
1479 bool LayerTreeImpl::SmoothnessTakesPriority() const {
1480 return layer_tree_host_impl_
->GetTreePriority() == SMOOTHNESS_TAKES_PRIORITY
;
1483 BlockingTaskRunner
* LayerTreeImpl::BlockingMainThreadTaskRunner() const {
1484 return proxy()->blocking_main_thread_task_runner();
1487 void LayerTreeImpl::SetPageScaleAnimation(
1488 const gfx::Vector2d
& target_offset
,
1491 base::TimeDelta duration
) {
1492 if (!InnerViewportScrollLayer())
1495 gfx::ScrollOffset scroll_total
= TotalScrollOffset();
1496 gfx::SizeF scaled_scrollable_size
= ScrollableSize();
1497 gfx::SizeF viewport_size
= InnerViewportContainerLayer()->bounds();
1499 // Easing constants experimentally determined.
1500 scoped_ptr
<TimingFunction
> timing_function
=
1501 CubicBezierTimingFunction::Create(.8, 0, .3, .9);
1503 // TODO(miletus) : Pass in ScrollOffset.
1504 page_scale_animation_
=
1505 PageScaleAnimation::Create(ScrollOffsetToVector2dF(scroll_total
),
1506 total_page_scale_factor(),
1508 scaled_scrollable_size
,
1509 timing_function
.Pass());
1512 gfx::Vector2dF
anchor(target_offset
);
1513 page_scale_animation_
->ZoomWithAnchor(anchor
,
1515 duration
.InSecondsF());
1517 gfx::Vector2dF scaled_target_offset
= target_offset
;
1518 page_scale_animation_
->ZoomTo(scaled_target_offset
,
1520 duration
.InSecondsF());
1524 scoped_ptr
<PageScaleAnimation
> LayerTreeImpl::TakePageScaleAnimation() {
1525 return page_scale_animation_
.Pass();