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"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/animation/keyframed_animation_curve.h"
14 #include "cc/animation/scrollbar_animation_controller.h"
15 #include "cc/animation/scrollbar_animation_controller_linear_fade.h"
16 #include "cc/animation/scrollbar_animation_controller_thinning.h"
17 #include "cc/base/math_util.h"
18 #include "cc/base/synced_property.h"
19 #include "cc/base/util.h"
20 #include "cc/debug/devtools_instrumentation.h"
21 #include "cc/debug/traced_value.h"
22 #include "cc/input/page_scale_animation.h"
23 #include "cc/layers/heads_up_display_layer_impl.h"
24 #include "cc/layers/layer.h"
25 #include "cc/layers/layer_iterator.h"
26 #include "cc/layers/render_surface_impl.h"
27 #include "cc/layers/scrollbar_layer_impl_base.h"
28 #include "cc/resources/ui_resource_request.h"
29 #include "cc/trees/layer_tree_host_common.h"
30 #include "cc/trees/layer_tree_host_impl.h"
31 #include "cc/trees/occlusion_tracker.h"
32 #include "ui/gfx/geometry/point_conversions.h"
33 #include "ui/gfx/geometry/size_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
37 // This class exists to split the LayerScrollOffsetDelegate between the
38 // InnerViewportScrollLayer and the OuterViewportScrollLayer in a manner
39 // that never requires the embedder or LayerImpl to know about.
40 class LayerScrollOffsetDelegateProxy
: public LayerImpl::ScrollOffsetDelegate
{
42 LayerScrollOffsetDelegateProxy(LayerImpl
* layer
,
43 LayerScrollOffsetDelegate
* delegate
,
44 LayerTreeImpl
* layer_tree
)
45 : layer_(layer
), delegate_(delegate
), layer_tree_impl_(layer_tree
) {}
46 virtual ~LayerScrollOffsetDelegateProxy() {}
48 gfx::ScrollOffset
last_set_scroll_offset() const {
49 return last_set_scroll_offset_
;
52 // LayerScrollOffsetDelegate implementation.
53 void SetCurrentScrollOffset(const gfx::ScrollOffset
& new_offset
) override
{
54 last_set_scroll_offset_
= new_offset
;
57 gfx::ScrollOffset
GetCurrentScrollOffset() override
{
58 return layer_tree_impl_
->GetDelegatedScrollOffset(layer_
);
61 bool IsExternalFlingActive() const override
{
62 return delegate_
->IsExternalFlingActive();
65 void Update() const override
{
66 layer_tree_impl_
->UpdateScrollOffsetDelegate();
71 LayerScrollOffsetDelegate
* delegate_
;
72 LayerTreeImpl
* layer_tree_impl_
;
73 gfx::ScrollOffset last_set_scroll_offset_
;
76 LayerTreeImpl::LayerTreeImpl(
77 LayerTreeHostImpl
* layer_tree_host_impl
,
78 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
79 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
80 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
)
81 : layer_tree_host_impl_(layer_tree_host_impl
),
82 source_frame_number_(-1),
84 currently_scrolling_layer_(NULL
),
85 root_layer_scroll_offset_delegate_(NULL
),
87 has_transparent_background_(false),
88 overscroll_elasticity_layer_(NULL
),
89 page_scale_layer_(NULL
),
90 inner_viewport_scroll_layer_(NULL
),
91 outer_viewport_scroll_layer_(NULL
),
92 page_scale_factor_(page_scale_factor
),
93 min_page_scale_factor_(0),
94 max_page_scale_factor_(0),
95 elastic_overscroll_(elastic_overscroll
),
96 scrolling_layer_id_from_previous_tree_(0),
97 contents_textures_purged_(false),
98 viewport_size_invalid_(false),
99 needs_update_draw_properties_(true),
100 needs_full_tree_sync_(true),
101 next_activation_forces_redraw_(false),
102 has_ever_been_drawn_(false),
103 render_surface_layer_list_id_(0),
104 top_controls_shrink_blink_size_(false),
105 top_controls_height_(0),
106 top_controls_shown_ratio_(top_controls_shown_ratio
) {
109 LayerTreeImpl::~LayerTreeImpl() {
110 BreakSwapPromises(SwapPromise::SWAP_FAILS
);
112 // Need to explicitly clear the tree prior to destroying this so that
113 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
114 DCHECK(!root_layer_
);
115 DCHECK(layers_with_copy_output_request_
.empty());
118 void LayerTreeImpl::Shutdown() {
119 root_layer_
= nullptr;
122 void LayerTreeImpl::ReleaseResources() {
124 LayerTreeHostCommon::CallFunctionForSubtree(
125 root_layer_
.get(), [](LayerImpl
* layer
) { layer
->ReleaseResources(); });
129 void LayerTreeImpl::RecreateResources() {
131 LayerTreeHostCommon::CallFunctionForSubtree(
133 [](LayerImpl
* layer
) { layer
->RecreateResources(); });
137 void LayerTreeImpl::GatherFrameTimingRequestIds(
138 std::vector
<int64_t>* request_ids
) {
142 // TODO(vmpstr): Early out if there are no requests on any of the layers. For
143 // that, we need to inform LayerTreeImpl whenever there are requests when we
145 LayerTreeHostCommon::CallFunctionForSubtree(
146 root_layer_
.get(), [request_ids
](LayerImpl
* layer
) {
147 layer
->GatherFrameTimingRequestIds(request_ids
);
151 void LayerTreeImpl::SetRootLayer(scoped_ptr
<LayerImpl
> layer
) {
152 if (inner_viewport_scroll_layer_
)
153 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
154 if (outer_viewport_scroll_layer_
)
155 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
156 inner_viewport_scroll_delegate_proxy_
= nullptr;
157 outer_viewport_scroll_delegate_proxy_
= nullptr;
159 root_layer_
= layer
.Pass();
160 currently_scrolling_layer_
= NULL
;
161 inner_viewport_scroll_layer_
= NULL
;
162 outer_viewport_scroll_layer_
= NULL
;
163 page_scale_layer_
= NULL
;
165 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
168 LayerImpl
* LayerTreeImpl::InnerViewportScrollLayer() const {
169 return inner_viewport_scroll_layer_
;
172 LayerImpl
* LayerTreeImpl::OuterViewportScrollLayer() const {
173 return outer_viewport_scroll_layer_
;
176 gfx::ScrollOffset
LayerTreeImpl::TotalScrollOffset() const {
177 gfx::ScrollOffset offset
;
179 if (inner_viewport_scroll_layer_
)
180 offset
+= inner_viewport_scroll_layer_
->CurrentScrollOffset();
182 if (outer_viewport_scroll_layer_
)
183 offset
+= outer_viewport_scroll_layer_
->CurrentScrollOffset();
188 gfx::ScrollOffset
LayerTreeImpl::TotalMaxScrollOffset() const {
189 gfx::ScrollOffset offset
;
191 if (inner_viewport_scroll_layer_
)
192 offset
+= inner_viewport_scroll_layer_
->MaxScrollOffset();
194 if (outer_viewport_scroll_layer_
)
195 offset
+= outer_viewport_scroll_layer_
->MaxScrollOffset();
200 scoped_ptr
<LayerImpl
> LayerTreeImpl::DetachLayerTree() {
201 // Clear all data structures that have direct references to the layer tree.
202 scrolling_layer_id_from_previous_tree_
=
203 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0;
204 if (inner_viewport_scroll_layer_
)
205 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
206 if (outer_viewport_scroll_layer_
)
207 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(NULL
);
208 inner_viewport_scroll_delegate_proxy_
= nullptr;
209 outer_viewport_scroll_delegate_proxy_
= nullptr;
210 inner_viewport_scroll_layer_
= NULL
;
211 outer_viewport_scroll_layer_
= NULL
;
212 page_scale_layer_
= NULL
;
213 currently_scrolling_layer_
= NULL
;
215 render_surface_layer_list_
.clear();
216 set_needs_update_draw_properties();
217 return root_layer_
.Pass();
220 void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl
* target_tree
) {
221 // The request queue should have been processed and does not require a push.
222 DCHECK_EQ(ui_resource_request_queue_
.size(), 0u);
224 if (next_activation_forces_redraw_
) {
225 target_tree
->ForceRedrawNextActivation();
226 next_activation_forces_redraw_
= false;
229 target_tree
->PassSwapPromises(&swap_promise_list_
);
231 target_tree
->set_top_controls_shrink_blink_size(
232 top_controls_shrink_blink_size_
);
233 target_tree
->set_top_controls_height(top_controls_height_
);
234 target_tree
->PushTopControls(nullptr);
236 // Active tree already shares the page_scale_factor object with pending
237 // tree so only the limits need to be provided.
238 target_tree
->PushPageScaleFactorAndLimits(nullptr, min_page_scale_factor(),
239 max_page_scale_factor());
240 target_tree
->elastic_overscroll()->PushPendingToActive();
242 target_tree
->pending_page_scale_animation_
=
243 pending_page_scale_animation_
.Pass();
245 if (page_scale_layer_
&& inner_viewport_scroll_layer_
) {
246 target_tree
->SetViewportLayersFromIds(
247 overscroll_elasticity_layer_
? overscroll_elasticity_layer_
->id()
249 page_scale_layer_
->id(), inner_viewport_scroll_layer_
->id(),
250 outer_viewport_scroll_layer_
? outer_viewport_scroll_layer_
->id()
251 : Layer::INVALID_ID
);
253 target_tree
->ClearViewportLayers();
256 target_tree
->RegisterSelection(selection_start_
, selection_end_
);
258 // This should match the property synchronization in
259 // LayerTreeHost::finishCommitOnImplThread().
260 target_tree
->set_source_frame_number(source_frame_number());
261 target_tree
->set_background_color(background_color());
262 target_tree
->set_has_transparent_background(has_transparent_background());
264 if (ContentsTexturesPurged())
265 target_tree
->SetContentsTexturesPurged();
267 target_tree
->ResetContentsTexturesPurged();
269 if (ViewportSizeInvalid())
270 target_tree
->SetViewportSizeInvalid();
272 target_tree
->ResetViewportSizeInvalid();
275 target_tree
->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl
*>(
276 LayerTreeHostCommon::FindLayerInSubtree(
277 target_tree
->root_layer(), hud_layer()->id())));
279 target_tree
->set_hud_layer(NULL
);
281 target_tree
->has_ever_been_drawn_
= false;
284 LayerImpl
* LayerTreeImpl::InnerViewportContainerLayer() const {
285 return inner_viewport_scroll_layer_
286 ? inner_viewport_scroll_layer_
->scroll_clip_layer()
290 LayerImpl
* LayerTreeImpl::OuterViewportContainerLayer() const {
291 return outer_viewport_scroll_layer_
292 ? outer_viewport_scroll_layer_
->scroll_clip_layer()
296 LayerImpl
* LayerTreeImpl::CurrentlyScrollingLayer() const {
297 DCHECK(IsActiveTree());
298 return currently_scrolling_layer_
;
301 void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl
* layer
) {
302 if (currently_scrolling_layer_
== layer
)
305 if (currently_scrolling_layer_
&&
306 currently_scrolling_layer_
->scrollbar_animation_controller())
307 currently_scrolling_layer_
->scrollbar_animation_controller()
309 currently_scrolling_layer_
= layer
;
310 if (layer
&& layer
->scrollbar_animation_controller())
311 layer
->scrollbar_animation_controller()->DidScrollBegin();
314 void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
315 SetCurrentlyScrollingLayer(NULL
);
316 scrolling_layer_id_from_previous_tree_
= 0;
321 void ForceScrollbarParameterUpdateAfterScaleChange(LayerImpl
* current_layer
) {
325 while (current_layer
) {
326 current_layer
->ScrollbarParametersDidChange(false);
327 current_layer
= current_layer
->parent();
333 float LayerTreeImpl::ClampPageScaleFactorToLimits(
334 float page_scale_factor
) const {
335 if (min_page_scale_factor_
&& page_scale_factor
< min_page_scale_factor_
)
336 page_scale_factor
= min_page_scale_factor_
;
337 else if (max_page_scale_factor_
&& page_scale_factor
> max_page_scale_factor_
)
338 page_scale_factor
= max_page_scale_factor_
;
339 return page_scale_factor
;
342 void LayerTreeImpl::SetPageScaleOnActiveTree(float active_page_scale
) {
343 DCHECK(IsActiveTree());
344 if (page_scale_factor()->SetCurrent(
345 ClampPageScaleFactorToLimits(active_page_scale
)))
346 DidUpdatePageScale();
349 void LayerTreeImpl::PushPageScaleFromMainThread(float page_scale_factor
,
350 float min_page_scale_factor
,
351 float max_page_scale_factor
) {
352 PushPageScaleFactorAndLimits(&page_scale_factor
, min_page_scale_factor
,
353 max_page_scale_factor
);
356 void LayerTreeImpl::PushPageScaleFactorAndLimits(const float* page_scale_factor
,
357 float min_page_scale_factor
,
358 float max_page_scale_factor
) {
359 DCHECK(page_scale_factor
|| IsActiveTree());
360 bool changed_page_scale
= false;
361 if (page_scale_factor
) {
362 DCHECK(!IsActiveTree() || !layer_tree_host_impl_
->pending_tree());
363 changed_page_scale
|=
364 page_scale_factor_
->PushFromMainThread(*page_scale_factor
);
367 changed_page_scale
|= page_scale_factor_
->PushPendingToActive();
368 changed_page_scale
|=
369 SetPageScaleFactorLimits(min_page_scale_factor
, max_page_scale_factor
);
371 if (changed_page_scale
)
372 DidUpdatePageScale();
375 void LayerTreeImpl::set_top_controls_shrink_blink_size(bool shrink
) {
376 if (top_controls_shrink_blink_size_
== shrink
)
379 top_controls_shrink_blink_size_
= shrink
;
381 layer_tree_host_impl_
->UpdateViewportContainerSizes();
384 void LayerTreeImpl::set_top_controls_height(float top_controls_height
) {
385 if (top_controls_height_
== top_controls_height
)
388 top_controls_height_
= top_controls_height
;
390 layer_tree_host_impl_
->UpdateViewportContainerSizes();
393 bool LayerTreeImpl::SetCurrentTopControlsShownRatio(float ratio
) {
394 ratio
= std::max(ratio
, 0.f
);
395 ratio
= std::min(ratio
, 1.f
);
396 return top_controls_shown_ratio_
->SetCurrent(ratio
);
399 void LayerTreeImpl::PushTopControlsFromMainThread(
400 float top_controls_shown_ratio
) {
401 PushTopControls(&top_controls_shown_ratio
);
404 void LayerTreeImpl::PushTopControls(const float* top_controls_shown_ratio
) {
405 DCHECK(top_controls_shown_ratio
|| IsActiveTree());
407 if (top_controls_shown_ratio
) {
408 DCHECK(!IsActiveTree() || !layer_tree_host_impl_
->pending_tree());
409 top_controls_shown_ratio_
->PushFromMainThread(*top_controls_shown_ratio
);
411 if (IsActiveTree()) {
412 if (top_controls_shown_ratio_
->PushPendingToActive())
413 layer_tree_host_impl_
->DidChangeTopControlsPosition();
417 bool LayerTreeImpl::SetPageScaleFactorLimits(float min_page_scale_factor
,
418 float max_page_scale_factor
) {
419 if (min_page_scale_factor
== min_page_scale_factor_
&&
420 max_page_scale_factor
== max_page_scale_factor_
)
423 min_page_scale_factor_
= min_page_scale_factor
;
424 max_page_scale_factor_
= max_page_scale_factor
;
429 void LayerTreeImpl::DidUpdatePageScale() {
431 page_scale_factor()->SetCurrent(
432 ClampPageScaleFactorToLimits(current_page_scale_factor()));
434 set_needs_update_draw_properties();
436 if (root_layer_scroll_offset_delegate_
) {
437 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
438 TotalScrollOffset(), TotalMaxScrollOffset(), ScrollableSize(),
439 current_page_scale_factor(), min_page_scale_factor_
,
440 max_page_scale_factor_
);
443 ForceScrollbarParameterUpdateAfterScaleChange(page_scale_layer());
445 HideInnerViewportScrollbarsIfNearMinimumScale();
448 void LayerTreeImpl::HideInnerViewportScrollbarsIfNearMinimumScale() {
449 if (!InnerViewportContainerLayer())
452 LayerImpl::ScrollbarSet
* scrollbars
=
453 InnerViewportContainerLayer()->scrollbars();
458 for (LayerImpl::ScrollbarSet::iterator it
= scrollbars
->begin();
459 it
!= scrollbars
->end();
461 ScrollbarLayerImplBase
* scrollbar
= *it
;
462 float minimum_scale_to_show_at
=
463 min_page_scale_factor() * settings().scrollbar_show_scale_threshold
;
464 scrollbar
->SetHideLayerAndSubtree(
465 current_page_scale_factor() < minimum_scale_to_show_at
);
469 SyncedProperty
<ScaleGroup
>* LayerTreeImpl::page_scale_factor() {
470 return page_scale_factor_
.get();
473 const SyncedProperty
<ScaleGroup
>* LayerTreeImpl::page_scale_factor() const {
474 return page_scale_factor_
.get();
477 gfx::SizeF
LayerTreeImpl::ScrollableViewportSize() const {
478 if (!InnerViewportContainerLayer())
481 return gfx::ScaleSize(InnerViewportContainerLayer()->BoundsForScrolling(),
482 1.0f
/ current_page_scale_factor());
485 gfx::Rect
LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
486 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
487 ? OuterViewportScrollLayer()
488 : InnerViewportScrollLayer();
489 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
491 LayerImpl
* layer
= root_scroll_layer
->children()[0];
492 return MathUtil::MapEnclosingClippedRect(layer
->screen_space_transform(),
493 gfx::Rect(layer
->content_bounds()));
496 void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
497 DCHECK(IsActiveTree());
499 page_scale_factor()->AbortCommit();
500 top_controls_shown_ratio()->AbortCommit();
501 elastic_overscroll()->AbortCommit();
506 LayerTreeHostCommon::CallFunctionForSubtree(
507 root_layer(), [](LayerImpl
* layer
) {
508 layer
->ApplySentScrollDeltasFromAbortedCommit();
512 void LayerTreeImpl::SetViewportLayersFromIds(
513 int overscroll_elasticity_layer_id
,
514 int page_scale_layer_id
,
515 int inner_viewport_scroll_layer_id
,
516 int outer_viewport_scroll_layer_id
) {
517 overscroll_elasticity_layer_
= LayerById(overscroll_elasticity_layer_id
);
518 page_scale_layer_
= LayerById(page_scale_layer_id
);
519 DCHECK(page_scale_layer_
);
521 inner_viewport_scroll_layer_
=
522 LayerById(inner_viewport_scroll_layer_id
);
523 DCHECK(inner_viewport_scroll_layer_
);
525 outer_viewport_scroll_layer_
=
526 LayerById(outer_viewport_scroll_layer_id
);
527 DCHECK(outer_viewport_scroll_layer_
||
528 outer_viewport_scroll_layer_id
== Layer::INVALID_ID
);
530 HideInnerViewportScrollbarsIfNearMinimumScale();
532 if (!root_layer_scroll_offset_delegate_
)
535 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
536 new LayerScrollOffsetDelegateProxy(inner_viewport_scroll_layer_
,
537 root_layer_scroll_offset_delegate_
,
540 if (outer_viewport_scroll_layer_
)
541 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
542 new LayerScrollOffsetDelegateProxy(outer_viewport_scroll_layer_
,
543 root_layer_scroll_offset_delegate_
,
547 void LayerTreeImpl::ClearViewportLayers() {
548 page_scale_layer_
= NULL
;
549 inner_viewport_scroll_layer_
= NULL
;
550 outer_viewport_scroll_layer_
= NULL
;
553 bool LayerTreeImpl::UpdateDrawProperties(bool update_lcd_text
) {
554 if (!needs_update_draw_properties_
)
557 // Calling UpdateDrawProperties must clear this flag, so there can be no
558 // early outs before this.
559 needs_update_draw_properties_
= false;
561 // For max_texture_size. When the renderer is re-created in
562 // CreateAndSetRenderer, the needs update draw properties flag is set
564 if (!layer_tree_host_impl_
->renderer())
567 // Clear this after the renderer early out, as it should still be
568 // possible to hit test even without a renderer.
569 render_surface_layer_list_
.clear();
576 "cc", "LayerTreeImpl::UpdateDrawProperties::CalculateDrawProperties",
577 "IsActive", IsActiveTree(), "SourceFrameNumber", source_frame_number_
);
578 LayerImpl
* page_scale_layer
=
579 page_scale_layer_
? page_scale_layer_
: InnerViewportContainerLayer();
580 bool can_render_to_separate_surface
=
581 (layer_tree_host_impl_
->GetDrawMode() !=
582 DRAW_MODE_RESOURCELESS_SOFTWARE
);
584 ++render_surface_layer_list_id_
;
585 LayerTreeHostCommon::CalcDrawPropsImplInputs
inputs(
586 root_layer(), DrawViewportSize(),
587 layer_tree_host_impl_
->DrawTransform(), device_scale_factor(),
588 current_page_scale_factor(), page_scale_layer
,
589 elastic_overscroll()->Current(IsActiveTree()),
590 overscroll_elasticity_layer_
, resource_provider()->max_texture_size(),
591 settings().can_use_lcd_text
, settings().layers_always_allowed_lcd_text
,
592 can_render_to_separate_surface
,
593 settings().layer_transforms_should_scale_layer_contents
,
594 settings().verify_property_trees
,
595 &render_surface_layer_list_
, render_surface_layer_list_id_
);
596 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
600 TRACE_EVENT2("cc", "LayerTreeImpl::UpdateDrawProperties::Occlusion",
601 "IsActive", IsActiveTree(), "SourceFrameNumber",
602 source_frame_number_
);
603 OcclusionTracker
<LayerImpl
> occlusion_tracker(
604 root_layer()->render_surface()->content_rect());
605 occlusion_tracker
.set_minimum_tracking_size(
606 settings().minimum_occlusion_tracking_size
);
608 // LayerIterator is used here instead of CallFunctionForSubtree to only
609 // UpdateTilePriorities on layers that will be visible (and thus have valid
610 // draw properties) and not because any ordering is required.
611 auto end
= LayerIterator
<LayerImpl
>::End(&render_surface_layer_list_
);
612 for (auto it
= LayerIterator
<LayerImpl
>::Begin(&render_surface_layer_list_
);
614 occlusion_tracker
.EnterLayer(it
);
616 // There are very few render targets so this should be cheap to do for
617 // each layer instead of something more complicated.
618 bool inside_replica
= false;
619 LayerImpl
* layer
= it
->render_target();
620 while (layer
&& !inside_replica
) {
621 if (layer
->render_target()->has_replica())
622 inside_replica
= true;
623 layer
= layer
->render_target()->parent();
626 // Don't use occlusion if a layer will appear in a replica, since the
627 // tile raster code does not know how to look for the replica and would
628 // consider it occluded even though the replica is visible.
629 // Since occlusion is only used for browser compositor (i.e.
630 // use_occlusion_for_tile_prioritization) and it won't use replicas,
631 // this should matter not.
633 if (it
.represents_itself()) {
634 Occlusion occlusion
=
635 inside_replica
? Occlusion()
636 : occlusion_tracker
.GetCurrentOcclusionForLayer(
637 it
->draw_transform());
638 it
->draw_properties().occlusion_in_content_space
= occlusion
;
641 if (it
.represents_contributing_render_surface()) {
642 // Surfaces aren't used by the tile raster code, so they can have
643 // occlusion regardless of replicas.
644 Occlusion occlusion
=
645 occlusion_tracker
.GetCurrentOcclusionForContributingSurface(
646 it
->render_surface()->draw_transform());
647 it
->render_surface()->set_occlusion_in_content_space(occlusion
);
648 // Masks are used to draw the contributing surface, so should have
649 // the same occlusion as the surface (nothing inside the surface
651 if (LayerImpl
* mask
= it
->mask_layer()) {
652 Occlusion mask_occlusion
=
655 : occlusion_tracker
.GetCurrentOcclusionForContributingSurface(
656 it
->render_surface()->draw_transform() *
657 it
->draw_transform());
658 mask
->draw_properties().occlusion_in_content_space
= mask_occlusion
;
660 if (LayerImpl
* replica
= it
->replica_layer()) {
661 if (LayerImpl
* mask
= replica
->mask_layer())
662 mask
->draw_properties().occlusion_in_content_space
= Occlusion();
666 occlusion_tracker
.LeaveLayer(it
);
669 unoccluded_screen_space_region_
=
670 occlusion_tracker
.ComputeVisibleRegionInScreen();
673 // It'd be ideal if this could be done earlier, but when the raster source
674 // is updated from the main thread during push properties, update draw
675 // properties has not occurred yet and so it's not clear whether or not the
676 // layer can or cannot use lcd text. So, this is the cleanup pass to
677 // determine if the raster source needs to be replaced with a non-lcd
678 // raster source due to draw properties.
679 if (update_lcd_text
) {
680 // TODO(enne): Make LTHI::sync_tree return this value.
681 LayerTreeImpl
* sync_tree
=
682 layer_tree_host_impl_
->proxy()->CommitToActiveTree()
683 ? layer_tree_host_impl_
->active_tree()
684 : layer_tree_host_impl_
->pending_tree();
685 // If this is not the sync tree, then it is not safe to update lcd text
686 // as it causes invalidations and the tiles may be in use.
687 DCHECK_EQ(this, sync_tree
);
688 for (const auto& layer
: picture_layers_
)
689 layer
->UpdateCanUseLCDTextAfterCommit();
693 TRACE_EVENT_BEGIN2("cc", "LayerTreeImpl::UpdateDrawProperties::UpdateTiles",
694 "IsActive", IsActiveTree(), "SourceFrameNumber",
695 source_frame_number_
);
696 const bool resourceless_software_draw
=
697 (layer_tree_host_impl_
->GetDrawMode() ==
698 DRAW_MODE_RESOURCELESS_SOFTWARE
);
699 size_t layers_updated_count
= 0;
700 bool tile_priorities_updated
= false;
701 for (PictureLayerImpl
* layer
: picture_layers_
) {
702 if (!layer
->IsDrawnRenderSurfaceLayerListMember())
704 ++layers_updated_count
;
705 tile_priorities_updated
|= layer
->UpdateTiles(resourceless_software_draw
);
708 if (tile_priorities_updated
)
709 DidModifyTilePriorities();
711 TRACE_EVENT_END1("cc", "LayerTreeImpl::UpdateTilePriorities",
712 "layers_updated_count", layers_updated_count
);
715 DCHECK(!needs_update_draw_properties_
) <<
716 "CalcDrawProperties should not set_needs_update_draw_properties()";
720 const LayerImplList
& LayerTreeImpl::RenderSurfaceLayerList() const {
721 // If this assert triggers, then the list is dirty.
722 DCHECK(!needs_update_draw_properties_
);
723 return render_surface_layer_list_
;
726 const Region
& LayerTreeImpl::UnoccludedScreenSpaceRegion() const {
727 // If this assert triggers, then the render_surface_layer_list_ is dirty, so
728 // the unoccluded_screen_space_region_ is not valid anymore.
729 DCHECK(!needs_update_draw_properties_
);
730 return unoccluded_screen_space_region_
;
733 gfx::Size
LayerTreeImpl::ScrollableSize() const {
734 LayerImpl
* root_scroll_layer
= OuterViewportScrollLayer()
735 ? OuterViewportScrollLayer()
736 : InnerViewportScrollLayer();
737 if (!root_scroll_layer
|| root_scroll_layer
->children().empty())
739 return root_scroll_layer
->children()[0]->bounds();
742 LayerImpl
* LayerTreeImpl::LayerById(int id
) {
743 LayerIdMap::iterator iter
= layer_id_map_
.find(id
);
744 return iter
!= layer_id_map_
.end() ? iter
->second
: NULL
;
747 void LayerTreeImpl::RegisterLayer(LayerImpl
* layer
) {
748 DCHECK(!LayerById(layer
->id()));
749 layer_id_map_
[layer
->id()] = layer
;
752 void LayerTreeImpl::UnregisterLayer(LayerImpl
* layer
) {
753 DCHECK(LayerById(layer
->id()));
754 layer_id_map_
.erase(layer
->id());
757 size_t LayerTreeImpl::NumLayers() {
758 return layer_id_map_
.size();
761 void LayerTreeImpl::PushPersistedState(LayerTreeImpl
* pending_tree
) {
762 pending_tree
->SetCurrentlyScrollingLayer(
763 LayerTreeHostCommon::FindLayerInSubtree(pending_tree
->root_layer(),
764 currently_scrolling_layer_
? currently_scrolling_layer_
->id() : 0));
767 void LayerTreeImpl::DidBecomeActive() {
768 if (next_activation_forces_redraw_
) {
769 layer_tree_host_impl_
->SetFullRootLayerDamage();
770 next_activation_forces_redraw_
= false;
773 if (scrolling_layer_id_from_previous_tree_
) {
774 currently_scrolling_layer_
= LayerTreeHostCommon::FindLayerInSubtree(
775 root_layer(), scrolling_layer_id_from_previous_tree_
);
778 // Always reset this flag on activation, as we would only have activated
779 // if we were in a good state.
780 layer_tree_host_impl_
->ResetRequiresHighResToDraw();
783 LayerTreeHostCommon::CallFunctionForSubtree(
784 root_layer(), [](LayerImpl
* layer
) { layer
->DidBecomeActive(); });
787 devtools_instrumentation::DidActivateLayerTree(layer_tree_host_impl_
->id(),
788 source_frame_number_
);
791 bool LayerTreeImpl::ContentsTexturesPurged() const {
792 return contents_textures_purged_
;
795 void LayerTreeImpl::SetContentsTexturesPurged() {
796 if (contents_textures_purged_
)
798 contents_textures_purged_
= true;
799 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
802 void LayerTreeImpl::ResetContentsTexturesPurged() {
803 if (!contents_textures_purged_
)
805 contents_textures_purged_
= false;
806 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
809 bool LayerTreeImpl::RequiresHighResToDraw() const {
810 return layer_tree_host_impl_
->RequiresHighResToDraw();
813 bool LayerTreeImpl::ViewportSizeInvalid() const {
814 return viewport_size_invalid_
;
817 void LayerTreeImpl::SetViewportSizeInvalid() {
818 viewport_size_invalid_
= true;
819 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
822 void LayerTreeImpl::ResetViewportSizeInvalid() {
823 viewport_size_invalid_
= false;
824 layer_tree_host_impl_
->OnCanDrawStateChangedForTree();
827 Proxy
* LayerTreeImpl::proxy() const {
828 return layer_tree_host_impl_
->proxy();
831 const LayerTreeSettings
& LayerTreeImpl::settings() const {
832 return layer_tree_host_impl_
->settings();
835 const LayerTreeDebugState
& LayerTreeImpl::debug_state() const {
836 return layer_tree_host_impl_
->debug_state();
839 const RendererCapabilitiesImpl
& LayerTreeImpl::GetRendererCapabilities() const {
840 return layer_tree_host_impl_
->GetRendererCapabilities();
843 ContextProvider
* LayerTreeImpl::context_provider() const {
844 return output_surface()->context_provider();
847 OutputSurface
* LayerTreeImpl::output_surface() const {
848 return layer_tree_host_impl_
->output_surface();
851 ResourceProvider
* LayerTreeImpl::resource_provider() const {
852 return layer_tree_host_impl_
->resource_provider();
855 TileManager
* LayerTreeImpl::tile_manager() const {
856 return layer_tree_host_impl_
->tile_manager();
859 FrameRateCounter
* LayerTreeImpl::frame_rate_counter() const {
860 return layer_tree_host_impl_
->fps_counter();
863 PaintTimeCounter
* LayerTreeImpl::paint_time_counter() const {
864 return layer_tree_host_impl_
->paint_time_counter();
867 MemoryHistory
* LayerTreeImpl::memory_history() const {
868 return layer_tree_host_impl_
->memory_history();
871 gfx::Size
LayerTreeImpl::device_viewport_size() const {
872 return layer_tree_host_impl_
->device_viewport_size();
875 float LayerTreeImpl::device_scale_factor() const {
876 return layer_tree_host_impl_
->device_scale_factor();
879 DebugRectHistory
* LayerTreeImpl::debug_rect_history() const {
880 return layer_tree_host_impl_
->debug_rect_history();
883 bool LayerTreeImpl::IsActiveTree() const {
884 return layer_tree_host_impl_
->active_tree() == this;
887 bool LayerTreeImpl::IsPendingTree() const {
888 return layer_tree_host_impl_
->pending_tree() == this;
891 bool LayerTreeImpl::IsRecycleTree() const {
892 return layer_tree_host_impl_
->recycle_tree() == this;
895 bool LayerTreeImpl::IsSyncTree() const {
896 return layer_tree_host_impl_
->sync_tree() == this;
899 LayerImpl
* LayerTreeImpl::FindActiveTreeLayerById(int id
) {
900 LayerTreeImpl
* tree
= layer_tree_host_impl_
->active_tree();
903 return tree
->LayerById(id
);
906 LayerImpl
* LayerTreeImpl::FindPendingTreeLayerById(int id
) {
907 LayerTreeImpl
* tree
= layer_tree_host_impl_
->pending_tree();
910 return tree
->LayerById(id
);
913 bool LayerTreeImpl::PinchGestureActive() const {
914 return layer_tree_host_impl_
->pinch_gesture_active();
917 BeginFrameArgs
LayerTreeImpl::CurrentBeginFrameArgs() const {
918 return layer_tree_host_impl_
->CurrentBeginFrameArgs();
921 base::TimeDelta
LayerTreeImpl::begin_impl_frame_interval() const {
922 return layer_tree_host_impl_
->begin_impl_frame_interval();
925 void LayerTreeImpl::SetNeedsCommit() {
926 layer_tree_host_impl_
->SetNeedsCommit();
929 gfx::Rect
LayerTreeImpl::DeviceViewport() const {
930 return layer_tree_host_impl_
->DeviceViewport();
933 gfx::Size
LayerTreeImpl::DrawViewportSize() const {
934 return layer_tree_host_impl_
->DrawViewportSize();
937 const gfx::Rect
LayerTreeImpl::ViewportRectForTilePriority() const {
938 return layer_tree_host_impl_
->ViewportRectForTilePriority();
941 scoped_ptr
<ScrollbarAnimationController
>
942 LayerTreeImpl::CreateScrollbarAnimationController(LayerImpl
* scrolling_layer
) {
943 DCHECK(settings().scrollbar_fade_delay_ms
);
944 DCHECK(settings().scrollbar_fade_duration_ms
);
945 base::TimeDelta delay
=
946 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_delay_ms
);
947 base::TimeDelta resize_delay
= base::TimeDelta::FromMilliseconds(
948 settings().scrollbar_fade_resize_delay_ms
);
949 base::TimeDelta duration
=
950 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_duration_ms
);
951 switch (settings().scrollbar_animator
) {
952 case LayerTreeSettings::LINEAR_FADE
: {
953 return ScrollbarAnimationControllerLinearFade::Create(
955 layer_tree_host_impl_
,
960 case LayerTreeSettings::THINNING
: {
961 return ScrollbarAnimationControllerThinning::Create(scrolling_layer
,
962 layer_tree_host_impl_
,
967 case LayerTreeSettings::NO_ANIMATOR
:
974 void LayerTreeImpl::DidAnimateScrollOffset() {
975 layer_tree_host_impl_
->DidAnimateScrollOffset();
978 bool LayerTreeImpl::use_gpu_rasterization() const {
979 return layer_tree_host_impl_
->use_gpu_rasterization();
982 GpuRasterizationStatus
LayerTreeImpl::GetGpuRasterizationStatus() const {
983 return layer_tree_host_impl_
->gpu_rasterization_status();
986 bool LayerTreeImpl::create_low_res_tiling() const {
987 return layer_tree_host_impl_
->create_low_res_tiling();
990 void LayerTreeImpl::SetNeedsRedraw() {
991 layer_tree_host_impl_
->SetNeedsRedraw();
994 AnimationRegistrar
* LayerTreeImpl::GetAnimationRegistrar() const {
995 return layer_tree_host_impl_
->animation_registrar();
998 void LayerTreeImpl::GetAllTilesAndPrioritiesForTracing(
999 std::map
<const Tile
*, TilePriority
>* tile_map
) const {
1000 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
1001 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
1002 for (LayerIteratorType it
=
1003 LayerIteratorType::Begin(&render_surface_layer_list_
);
1006 if (!it
.represents_itself())
1008 LayerImpl
* layer_impl
= *it
;
1009 layer_impl
->GetAllTilesAndPrioritiesForTracing(tile_map
);
1013 void LayerTreeImpl::AsValueInto(base::trace_event::TracedValue
* state
) const {
1014 TracedValue::MakeDictIntoImplicitSnapshot(state
, "cc::LayerTreeImpl", this);
1015 state
->SetInteger("source_frame_number", source_frame_number_
);
1017 state
->BeginDictionary("root_layer");
1018 root_layer_
->AsValueInto(state
);
1019 state
->EndDictionary();
1021 state
->BeginArray("render_surface_layer_list");
1022 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
1023 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list_
);
1024 for (LayerIteratorType it
= LayerIteratorType::Begin(
1025 &render_surface_layer_list_
); it
!= end
; ++it
) {
1026 if (!it
.represents_itself())
1028 TracedValue::AppendIDRef(*it
, state
);
1032 state
->BeginArray("swap_promise_trace_ids");
1033 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
1034 state
->AppendDouble(swap_promise_list_
[i
]->TraceId());
1038 void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
1039 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
) {
1040 if (root_layer_scroll_offset_delegate_
== root_layer_scroll_offset_delegate
)
1043 if (!root_layer_scroll_offset_delegate
) {
1044 // Make sure we remove the proxies from their layers before
1046 if (InnerViewportScrollLayer())
1047 InnerViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
1048 if (OuterViewportScrollLayer())
1049 OuterViewportScrollLayer()->SetScrollOffsetDelegate(NULL
);
1050 inner_viewport_scroll_delegate_proxy_
= nullptr;
1051 outer_viewport_scroll_delegate_proxy_
= nullptr;
1054 root_layer_scroll_offset_delegate_
= root_layer_scroll_offset_delegate
;
1056 if (root_layer_scroll_offset_delegate_
) {
1057 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
1058 TotalScrollOffset(), TotalMaxScrollOffset(), ScrollableSize(),
1059 current_page_scale_factor(), min_page_scale_factor(),
1060 max_page_scale_factor());
1062 if (inner_viewport_scroll_layer_
) {
1063 inner_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
1064 new LayerScrollOffsetDelegateProxy(InnerViewportScrollLayer(),
1065 root_layer_scroll_offset_delegate_
,
1067 inner_viewport_scroll_layer_
->SetScrollOffsetDelegate(
1068 inner_viewport_scroll_delegate_proxy_
.get());
1071 if (outer_viewport_scroll_layer_
) {
1072 outer_viewport_scroll_delegate_proxy_
= make_scoped_ptr(
1073 new LayerScrollOffsetDelegateProxy(OuterViewportScrollLayer(),
1074 root_layer_scroll_offset_delegate_
,
1076 outer_viewport_scroll_layer_
->SetScrollOffsetDelegate(
1077 outer_viewport_scroll_delegate_proxy_
.get());
1080 if (inner_viewport_scroll_layer_
)
1081 inner_viewport_scroll_layer_
->RefreshFromScrollDelegate();
1082 if (outer_viewport_scroll_layer_
)
1083 outer_viewport_scroll_layer_
->RefreshFromScrollDelegate();
1085 if (inner_viewport_scroll_layer_
)
1086 UpdateScrollOffsetDelegate();
1090 void LayerTreeImpl::OnRootLayerDelegatedScrollOffsetChanged() {
1091 DCHECK(root_layer_scroll_offset_delegate_
);
1092 if (inner_viewport_scroll_layer_
) {
1093 inner_viewport_scroll_layer_
->RefreshFromScrollDelegate();
1095 if (outer_viewport_scroll_layer_
) {
1096 outer_viewport_scroll_layer_
->RefreshFromScrollDelegate();
1100 void LayerTreeImpl::UpdateScrollOffsetDelegate() {
1101 DCHECK(InnerViewportScrollLayer());
1102 DCHECK(!OuterViewportScrollLayer() || outer_viewport_scroll_delegate_proxy_
);
1103 DCHECK(root_layer_scroll_offset_delegate_
);
1105 gfx::ScrollOffset offset
=
1106 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
1108 if (OuterViewportScrollLayer())
1109 offset
+= outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
1111 root_layer_scroll_offset_delegate_
->UpdateRootLayerState(
1112 offset
, TotalMaxScrollOffset(), ScrollableSize(),
1113 current_page_scale_factor(), min_page_scale_factor(),
1114 max_page_scale_factor());
1117 gfx::ScrollOffset
LayerTreeImpl::GetDelegatedScrollOffset(LayerImpl
* layer
) {
1118 DCHECK(root_layer_scroll_offset_delegate_
);
1119 DCHECK(InnerViewportScrollLayer());
1120 if (layer
== InnerViewportScrollLayer() && !OuterViewportScrollLayer())
1121 return root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
1123 // If we get here, we have both inner/outer viewports, and need to distribute
1124 // the scroll offset between them.
1125 DCHECK(inner_viewport_scroll_delegate_proxy_
);
1126 DCHECK(outer_viewport_scroll_delegate_proxy_
);
1127 gfx::ScrollOffset inner_viewport_offset
=
1128 inner_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
1129 gfx::ScrollOffset outer_viewport_offset
=
1130 outer_viewport_scroll_delegate_proxy_
->last_set_scroll_offset();
1132 // It may be nothing has changed.
1133 gfx::ScrollOffset delegate_offset
=
1134 root_layer_scroll_offset_delegate_
->GetTotalScrollOffset();
1135 if (inner_viewport_offset
+ outer_viewport_offset
== delegate_offset
) {
1136 if (layer
== InnerViewportScrollLayer())
1137 return inner_viewport_offset
;
1139 return outer_viewport_offset
;
1142 gfx::ScrollOffset max_outer_viewport_scroll_offset
=
1143 OuterViewportScrollLayer()->MaxScrollOffset();
1145 outer_viewport_offset
= delegate_offset
- inner_viewport_offset
;
1146 outer_viewport_offset
.SetToMin(max_outer_viewport_scroll_offset
);
1147 outer_viewport_offset
.SetToMax(gfx::ScrollOffset());
1149 if (layer
== OuterViewportScrollLayer())
1150 return outer_viewport_offset
;
1152 inner_viewport_offset
= delegate_offset
- outer_viewport_offset
;
1154 return inner_viewport_offset
;
1157 void LayerTreeImpl::QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
) {
1158 DCHECK(swap_promise
);
1159 swap_promise_list_
.push_back(swap_promise
.Pass());
1162 void LayerTreeImpl::PassSwapPromises(
1163 ScopedPtrVector
<SwapPromise
>* new_swap_promise
) {
1164 swap_promise_list_
.insert_and_take(swap_promise_list_
.end(),
1166 new_swap_promise
->clear();
1169 void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata
* metadata
) {
1170 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
1171 swap_promise_list_
[i
]->DidSwap(metadata
);
1172 swap_promise_list_
.clear();
1175 void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason
) {
1176 for (size_t i
= 0; i
< swap_promise_list_
.size(); i
++)
1177 swap_promise_list_
[i
]->DidNotSwap(reason
);
1178 swap_promise_list_
.clear();
1181 void LayerTreeImpl::DidModifyTilePriorities() {
1182 layer_tree_host_impl_
->DidModifyTilePriorities();
1185 void LayerTreeImpl::set_ui_resource_request_queue(
1186 const UIResourceRequestQueue
& queue
) {
1187 ui_resource_request_queue_
= queue
;
1190 ResourceProvider::ResourceId
LayerTreeImpl::ResourceIdForUIResource(
1191 UIResourceId uid
) const {
1192 return layer_tree_host_impl_
->ResourceIdForUIResource(uid
);
1195 bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid
) const {
1196 return layer_tree_host_impl_
->IsUIResourceOpaque(uid
);
1199 void LayerTreeImpl::ProcessUIResourceRequestQueue() {
1200 for (const auto& req
: ui_resource_request_queue_
) {
1201 switch (req
.GetType()) {
1202 case UIResourceRequest::UI_RESOURCE_CREATE
:
1203 layer_tree_host_impl_
->CreateUIResource(req
.GetId(), req
.GetBitmap());
1205 case UIResourceRequest::UI_RESOURCE_DELETE
:
1206 layer_tree_host_impl_
->DeleteUIResource(req
.GetId());
1208 case UIResourceRequest::UI_RESOURCE_INVALID_REQUEST
:
1213 ui_resource_request_queue_
.clear();
1215 // If all UI resource evictions were not recreated by processing this queue,
1216 // then another commit is required.
1217 if (layer_tree_host_impl_
->EvictedUIResourcesExist())
1218 layer_tree_host_impl_
->SetNeedsCommit();
1221 void LayerTreeImpl::RegisterPictureLayerImpl(PictureLayerImpl
* layer
) {
1222 DCHECK(std::find(picture_layers_
.begin(), picture_layers_
.end(), layer
) ==
1223 picture_layers_
.end());
1224 picture_layers_
.push_back(layer
);
1227 void LayerTreeImpl::UnregisterPictureLayerImpl(PictureLayerImpl
* layer
) {
1228 std::vector
<PictureLayerImpl
*>::iterator it
=
1229 std::find(picture_layers_
.begin(), picture_layers_
.end(), layer
);
1230 DCHECK(it
!= picture_layers_
.end());
1231 picture_layers_
.erase(it
);
1234 void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl
* layer
) {
1235 // Only the active tree needs to know about layers with copy requests, as
1236 // they are aborted if not serviced during draw.
1237 DCHECK(IsActiveTree());
1239 // DCHECK(std::find(layers_with_copy_output_request_.begin(),
1240 // layers_with_copy_output_request_.end(),
1241 // layer) == layers_with_copy_output_request_.end());
1242 // TODO(danakj): Remove this once crash is found crbug.com/309777
1243 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
1244 CHECK(layers_with_copy_output_request_
[i
] != layer
)
1245 << i
<< " of " << layers_with_copy_output_request_
.size();
1247 layers_with_copy_output_request_
.push_back(layer
);
1250 void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
) {
1251 // Only the active tree needs to know about layers with copy requests, as
1252 // they are aborted if not serviced during draw.
1253 DCHECK(IsActiveTree());
1255 std::vector
<LayerImpl
*>::iterator it
= std::find(
1256 layers_with_copy_output_request_
.begin(),
1257 layers_with_copy_output_request_
.end(),
1259 DCHECK(it
!= layers_with_copy_output_request_
.end());
1260 layers_with_copy_output_request_
.erase(it
);
1262 // TODO(danakj): Remove this once crash is found crbug.com/309777
1263 for (size_t i
= 0; i
< layers_with_copy_output_request_
.size(); ++i
) {
1264 CHECK(layers_with_copy_output_request_
[i
] != layer
)
1265 << i
<< " of " << layers_with_copy_output_request_
.size();
1269 const std::vector
<LayerImpl
*>& LayerTreeImpl::LayersWithCopyOutputRequest()
1271 // Only the active tree needs to know about layers with copy requests, as
1272 // they are aborted if not serviced during draw.
1273 DCHECK(IsActiveTree());
1275 return layers_with_copy_output_request_
;
1278 template <typename LayerType
>
1279 static inline bool LayerClipsSubtree(LayerType
* layer
) {
1280 return layer
->masks_to_bounds() || layer
->mask_layer();
1283 static bool PointHitsRect(
1284 const gfx::PointF
& screen_space_point
,
1285 const gfx::Transform
& local_space_to_screen_space_transform
,
1286 const gfx::RectF
& local_space_rect
,
1287 float* distance_to_camera
) {
1288 // If the transform is not invertible, then assume that this point doesn't hit
1290 gfx::Transform
inverse_local_space_to_screen_space(
1291 gfx::Transform::kSkipInitialization
);
1292 if (!local_space_to_screen_space_transform
.GetInverse(
1293 &inverse_local_space_to_screen_space
))
1296 // Transform the hit test point from screen space to the local space of the
1298 bool clipped
= false;
1299 gfx::Point3F planar_point
= MathUtil::ProjectPoint3D(
1300 inverse_local_space_to_screen_space
, screen_space_point
, &clipped
);
1301 gfx::PointF hit_test_point_in_local_space
=
1302 gfx::PointF(planar_point
.x(), planar_point
.y());
1304 // If ProjectPoint could not project to a valid value, then we assume that
1305 // this point doesn't hit this rect.
1309 if (!local_space_rect
.Contains(hit_test_point_in_local_space
))
1312 if (distance_to_camera
) {
1313 // To compute the distance to the camera, we have to take the planar point
1314 // and pull it back to world space and compute the displacement along the
1316 gfx::Point3F
planar_point_in_screen_space(planar_point
);
1317 local_space_to_screen_space_transform
.TransformPoint(
1318 &planar_point_in_screen_space
);
1319 *distance_to_camera
= planar_point_in_screen_space
.z();
1325 static bool PointHitsRegion(const gfx::PointF
& screen_space_point
,
1326 const gfx::Transform
& screen_space_transform
,
1327 const Region
& layer_space_region
,
1328 float layer_content_scale_x
,
1329 float layer_content_scale_y
) {
1330 // If the transform is not invertible, then assume that this point doesn't hit
1332 gfx::Transform
inverse_screen_space_transform(
1333 gfx::Transform::kSkipInitialization
);
1334 if (!screen_space_transform
.GetInverse(&inverse_screen_space_transform
))
1337 // Transform the hit test point from screen space to the local space of the
1339 bool clipped
= false;
1340 gfx::PointF hit_test_point_in_content_space
= MathUtil::ProjectPoint(
1341 inverse_screen_space_transform
, screen_space_point
, &clipped
);
1342 gfx::PointF hit_test_point_in_layer_space
=
1343 gfx::ScalePoint(hit_test_point_in_content_space
,
1344 1.f
/ layer_content_scale_x
,
1345 1.f
/ layer_content_scale_y
);
1347 // If ProjectPoint could not project to a valid value, then we assume that
1348 // this point doesn't hit this region.
1352 return layer_space_region
.Contains(
1353 gfx::ToRoundedPoint(hit_test_point_in_layer_space
));
1356 static const LayerImpl
* GetNextClippingLayer(const LayerImpl
* layer
) {
1357 if (layer
->scroll_parent())
1358 return layer
->scroll_parent();
1359 if (layer
->clip_parent())
1360 return layer
->clip_parent();
1361 return layer
->parent();
1364 static bool PointIsClippedBySurfaceOrClipRect(
1365 const gfx::PointF
& screen_space_point
,
1366 const LayerImpl
* layer
) {
1367 // Walk up the layer tree and hit-test any render_surfaces and any layer
1368 // clip rects that are active.
1369 for (; layer
; layer
= GetNextClippingLayer(layer
)) {
1370 if (layer
->render_surface() &&
1371 !PointHitsRect(screen_space_point
,
1372 layer
->render_surface()->screen_space_transform(),
1373 layer
->render_surface()->content_rect(),
1377 if (LayerClipsSubtree(layer
) &&
1378 !PointHitsRect(screen_space_point
,
1379 layer
->screen_space_transform(),
1380 gfx::Rect(layer
->content_bounds()),
1385 // If we have finished walking all ancestors without having already exited,
1386 // then the point is not clipped by any ancestors.
1390 static bool PointHitsLayer(const LayerImpl
* layer
,
1391 const gfx::PointF
& screen_space_point
,
1392 float* distance_to_intersection
) {
1393 gfx::RectF
content_rect(layer
->content_bounds());
1394 if (!PointHitsRect(screen_space_point
,
1395 layer
->screen_space_transform(),
1397 distance_to_intersection
))
1400 // At this point, we think the point does hit the layer, but we need to walk
1401 // up the parents to ensure that the layer was not clipped in such a way
1402 // that the hit point actually should not hit the layer.
1403 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer
))
1406 // Skip the HUD layer.
1407 if (layer
== layer
->layer_tree_impl()->hud_layer())
1413 struct FindClosestMatchingLayerDataForRecursion
{
1414 FindClosestMatchingLayerDataForRecursion()
1415 : closest_match(NULL
),
1416 closest_distance(-std::numeric_limits
<float>::infinity()) {}
1417 LayerImpl
* closest_match
;
1418 // Note that the positive z-axis points towards the camera, so bigger means
1419 // closer in this case, counterintuitively.
1420 float closest_distance
;
1423 template <typename Functor
>
1424 static void FindClosestMatchingLayer(
1425 const gfx::PointF
& screen_space_point
,
1427 const Functor
& func
,
1428 FindClosestMatchingLayerDataForRecursion
* data_for_recursion
) {
1429 for (int i
= layer
->children().size() - 1; i
>= 0; --i
) {
1430 FindClosestMatchingLayer(
1431 screen_space_point
, layer
->children()[i
], func
, data_for_recursion
);
1434 float distance_to_intersection
= 0.f
;
1436 PointHitsLayer(layer
, screen_space_point
, &distance_to_intersection
) &&
1437 ((!data_for_recursion
->closest_match
||
1438 distance_to_intersection
> data_for_recursion
->closest_distance
))) {
1439 data_for_recursion
->closest_distance
= distance_to_intersection
;
1440 data_for_recursion
->closest_match
= layer
;
1444 static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl
* layer
) {
1445 if (!layer
->scrollable())
1447 if (layer
->draw_properties().layer_or_descendant_is_drawn
)
1450 if (!layer
->scroll_children())
1452 for (std::set
<LayerImpl
*>::const_iterator it
=
1453 layer
->scroll_children()->begin();
1454 it
!= layer
->scroll_children()->end();
1456 if ((*it
)->draw_properties().layer_or_descendant_is_drawn
)
1462 struct FindScrollingLayerFunctor
{
1463 bool operator()(LayerImpl
* layer
) const {
1464 return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
);
1468 LayerImpl
* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint(
1469 const gfx::PointF
& screen_space_point
) {
1470 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1471 FindClosestMatchingLayer(screen_space_point
,
1473 FindScrollingLayerFunctor(),
1474 &data_for_recursion
);
1475 return data_for_recursion
.closest_match
;
1478 struct HitTestVisibleScrollableOrTouchableFunctor
{
1479 bool operator()(LayerImpl
* layer
) const {
1480 return layer
->IsDrawnRenderSurfaceLayerListMember() ||
1481 ScrollsAnyDrawnRenderSurfaceLayerListMember(layer
) ||
1482 !layer
->touch_event_handler_region().IsEmpty() ||
1483 layer
->have_wheel_event_handlers();
1487 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPoint(
1488 const gfx::PointF
& screen_space_point
) {
1491 bool update_lcd_text
= false;
1492 if (!UpdateDrawProperties(update_lcd_text
))
1494 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1495 FindClosestMatchingLayer(screen_space_point
,
1497 HitTestVisibleScrollableOrTouchableFunctor(),
1498 &data_for_recursion
);
1499 return data_for_recursion
.closest_match
;
1502 static bool LayerHasTouchEventHandlersAt(const gfx::PointF
& screen_space_point
,
1503 LayerImpl
* layer_impl
) {
1504 if (layer_impl
->touch_event_handler_region().IsEmpty())
1507 if (!PointHitsRegion(screen_space_point
,
1508 layer_impl
->screen_space_transform(),
1509 layer_impl
->touch_event_handler_region(),
1510 layer_impl
->contents_scale_x(),
1511 layer_impl
->contents_scale_y()))
1514 // At this point, we think the point does hit the touch event handler region
1515 // on the layer, but we need to walk up the parents to ensure that the layer
1516 // was not clipped in such a way that the hit point actually should not hit
1518 if (PointIsClippedBySurfaceOrClipRect(screen_space_point
, layer_impl
))
1524 struct FindWheelEventLayerFunctor
{
1525 bool operator()(LayerImpl
* layer
) const {
1526 return layer
->have_wheel_event_handlers();
1530 LayerImpl
* LayerTreeImpl::FindLayerWithWheelHandlerThatIsHitByPoint(
1531 const gfx::PointF
& screen_space_point
) {
1534 bool update_lcd_text
= false;
1535 if (!UpdateDrawProperties(update_lcd_text
))
1537 FindWheelEventLayerFunctor func
;
1538 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1539 FindClosestMatchingLayer(screen_space_point
, root_layer(), func
,
1540 &data_for_recursion
);
1541 return data_for_recursion
.closest_match
;
1544 struct FindTouchEventLayerFunctor
{
1545 bool operator()(LayerImpl
* layer
) const {
1546 return LayerHasTouchEventHandlersAt(screen_space_point
, layer
);
1548 const gfx::PointF screen_space_point
;
1551 LayerImpl
* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
1552 const gfx::PointF
& screen_space_point
) {
1555 bool update_lcd_text
= false;
1556 if (!UpdateDrawProperties(update_lcd_text
))
1558 FindTouchEventLayerFunctor func
= {screen_space_point
};
1559 FindClosestMatchingLayerDataForRecursion data_for_recursion
;
1560 FindClosestMatchingLayer(
1561 screen_space_point
, root_layer(), func
, &data_for_recursion
);
1562 return data_for_recursion
.closest_match
;
1565 void LayerTreeImpl::RegisterSelection(const LayerSelectionBound
& start
,
1566 const LayerSelectionBound
& end
) {
1567 selection_start_
= start
;
1568 selection_end_
= end
;
1571 static ViewportSelectionBound
ComputeViewportSelection(
1572 const LayerSelectionBound
& layer_bound
,
1574 float device_scale_factor
) {
1575 ViewportSelectionBound viewport_bound
;
1576 viewport_bound
.type
= layer_bound
.type
;
1578 if (!layer
|| layer_bound
.type
== SELECTION_BOUND_EMPTY
)
1579 return viewport_bound
;
1581 gfx::PointF layer_scaled_top
= gfx::ScalePoint(layer_bound
.edge_top
,
1582 layer
->contents_scale_x(),
1583 layer
->contents_scale_y());
1584 gfx::PointF layer_scaled_bottom
= gfx::ScalePoint(layer_bound
.edge_bottom
,
1585 layer
->contents_scale_x(),
1586 layer
->contents_scale_y());
1588 bool clipped
= false;
1589 gfx::PointF screen_top
= MathUtil::MapPoint(
1590 layer
->screen_space_transform(), layer_scaled_top
, &clipped
);
1591 gfx::PointF screen_bottom
= MathUtil::MapPoint(
1592 layer
->screen_space_transform(), layer_scaled_bottom
, &clipped
);
1594 const float inv_scale
= 1.f
/ device_scale_factor
;
1595 viewport_bound
.edge_top
= gfx::ScalePoint(screen_top
, inv_scale
);
1596 viewport_bound
.edge_bottom
= gfx::ScalePoint(screen_bottom
, inv_scale
);
1598 // The bottom edge point is used for visibility testing as it is the logical
1599 // focal point for bound selection handles (this may change in the future).
1600 // Shifting the visibility point fractionally inward ensures that neighboring
1601 // or logically coincident layers aligned to integral DPI coordinates will not
1602 // spuriously occlude the bound.
1603 gfx::Vector2dF visibility_offset
= layer_scaled_top
- layer_scaled_bottom
;
1604 visibility_offset
.Scale(device_scale_factor
/ visibility_offset
.Length());
1605 gfx::PointF visibility_point
= layer_scaled_bottom
+ visibility_offset
;
1606 if (visibility_point
.x() <= 0)
1607 visibility_point
.set_x(visibility_point
.x() + device_scale_factor
);
1608 visibility_point
= MathUtil::MapPoint(
1609 layer
->screen_space_transform(), visibility_point
, &clipped
);
1611 float intersect_distance
= 0.f
;
1612 viewport_bound
.visible
=
1613 PointHitsLayer(layer
, visibility_point
, &intersect_distance
);
1615 return viewport_bound
;
1618 void LayerTreeImpl::GetViewportSelection(ViewportSelectionBound
* start
,
1619 ViewportSelectionBound
* end
) {
1623 *start
= ComputeViewportSelection(
1625 selection_start_
.layer_id
? LayerById(selection_start_
.layer_id
) : NULL
,
1626 device_scale_factor());
1627 if (start
->type
== SELECTION_BOUND_CENTER
||
1628 start
->type
== SELECTION_BOUND_EMPTY
) {
1631 *end
= ComputeViewportSelection(
1633 selection_end_
.layer_id
? LayerById(selection_end_
.layer_id
) : NULL
,
1634 device_scale_factor());
1638 void LayerTreeImpl::InputScrollAnimationFinished() {
1639 layer_tree_host_impl_
->ScrollEnd();
1642 bool LayerTreeImpl::SmoothnessTakesPriority() const {
1643 return layer_tree_host_impl_
->GetTreePriority() == SMOOTHNESS_TAKES_PRIORITY
;
1646 BlockingTaskRunner
* LayerTreeImpl::BlockingMainThreadTaskRunner() const {
1647 return proxy()->blocking_main_thread_task_runner();
1650 void LayerTreeImpl::SetPendingPageScaleAnimation(
1651 scoped_ptr
<PendingPageScaleAnimation
> pending_animation
) {
1652 pending_page_scale_animation_
= pending_animation
.Pass();
1655 scoped_ptr
<PendingPageScaleAnimation
>
1656 LayerTreeImpl::TakePendingPageScaleAnimation() {
1657 return pending_page_scale_animation_
.Pass();