blob: 25f48d6fb4a4c466f1e42ecb83223389ec13d19b [file] [log] [blame]
[email protected]3b31c6ac2012-12-06 21:27:291// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_impl.h"
[email protected]3b31c6ac2012-12-06 21:27:296
aelias6004fe02015-02-07 21:43:017#include <algorithm>
[email protected]28336d52014-05-12 19:07:288#include <limits>
9#include <set>
10
primianoc06e2382015-01-28 04:21:4911#include "base/trace_event/trace_event.h"
12#include "base/trace_event/trace_event_argument.h"
[email protected]95e4e1a02013-03-18 07:09:0913#include "cc/animation/keyframed_animation_curve.h"
14#include "cc/animation/scrollbar_animation_controller.h"
[email protected]930ff43b2014-05-02 05:24:0015#include "cc/animation/scrollbar_animation_controller_linear_fade.h"
16#include "cc/animation/scrollbar_animation_controller_thinning.h"
[email protected]3744e27b2013-11-06 21:44:0817#include "cc/base/math_util.h"
aelias58eec0812014-12-04 01:04:4018#include "cc/base/synced_property.h"
[email protected]3744e27b2013-11-06 21:44:0819#include "cc/base/util.h"
[email protected]12a63da2014-06-13 06:06:2220#include "cc/debug/devtools_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2221#include "cc/debug/traced_value.h"
hush33370e12015-04-07 03:49:5022#include "cc/input/layer_scroll_offset_delegate.h"
bokan915bf352014-10-02 21:57:1423#include "cc/input/page_scale_animation.h"
[email protected]cc3cfaa2013-03-18 09:05:5224#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]57ac9482013-09-17 21:13:3925#include "cc/layers/layer.h"
[email protected]34ba1ffb2014-03-05 06:55:0326#include "cc/layers/layer_iterator.h"
[email protected]50761e92013-03-29 20:51:2827#include "cc/layers/render_surface_impl.h"
[email protected]80413d72013-08-30 20:25:3328#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]e1042192013-11-08 05:44:2429#include "cc/resources/ui_resource_request.h"
[email protected]556fd292013-03-18 08:03:0430#include "cc/trees/layer_tree_host_common.h"
31#include "cc/trees/layer_tree_host_impl.h"
[email protected]562b7ad2014-06-23 22:17:1132#include "cc/trees/occlusion_tracker.h"
heejin.r.chungd28506ba2014-10-23 16:36:2033#include "ui/gfx/geometry/point_conversions.h"
34#include "ui/gfx/geometry/size_conversions.h"
35#include "ui/gfx/geometry/vector2d_conversions.h"
[email protected]3b31c6ac2012-12-06 21:27:2936
37namespace cc {
[email protected]adeda572014-01-31 00:49:4738
aelias58eec0812014-12-04 01:04:4039LayerTreeImpl::LayerTreeImpl(
40 LayerTreeHostImpl* layer_tree_host_impl,
ccameronb9aec4502014-12-05 19:31:0041 scoped_refptr<SyncedProperty<ScaleGroup>> page_scale_factor,
aelias6004fe02015-02-07 21:43:0142 scoped_refptr<SyncedTopControls> top_controls_shown_ratio,
ccameronb9aec4502014-12-05 19:31:0043 scoped_refptr<SyncedElasticOverscroll> elastic_overscroll)
[email protected]db8259f2013-02-01 05:25:0444 : layer_tree_host_impl_(layer_tree_host_impl),
45 source_frame_number_(-1),
46 hud_layer_(0),
[email protected]1960a712013-04-30 17:06:4747 currently_scrolling_layer_(NULL),
48 root_layer_scroll_offset_delegate_(NULL),
[email protected]db8259f2013-02-01 05:25:0449 background_color_(0),
50 has_transparent_background_(false),
ccameronb9aec4502014-12-05 19:31:0051 overscroll_elasticity_layer_(NULL),
[email protected]57ac9482013-09-17 21:13:3952 page_scale_layer_(NULL),
53 inner_viewport_scroll_layer_(NULL),
54 outer_viewport_scroll_layer_(NULL),
aelias58eec0812014-12-04 01:04:4055 page_scale_factor_(page_scale_factor),
[email protected]db8259f2013-02-01 05:25:0456 min_page_scale_factor_(0),
57 max_page_scale_factor_(0),
ccameronb9aec4502014-12-05 19:31:0058 elastic_overscroll_(elastic_overscroll),
[email protected]db8259f2013-02-01 05:25:0459 scrolling_layer_id_from_previous_tree_(0),
60 contents_textures_purged_(false),
[email protected]318822852013-02-14 00:54:2761 viewport_size_invalid_(false),
[email protected]db8259f2013-02-01 05:25:0462 needs_update_draw_properties_(true),
[email protected]7d08a9352013-10-15 08:24:5663 needs_full_tree_sync_(true),
[email protected]390bb1ff2014-05-09 17:14:4064 next_activation_forces_redraw_(false),
[email protected]759dc9f2014-07-23 19:18:5165 has_ever_been_drawn_(false),
bokan88eae012014-09-09 20:40:4266 render_surface_layer_list_id_(0),
dtrainorcb7779b82014-12-04 01:08:0267 top_controls_shrink_blink_size_(false),
68 top_controls_height_(0),
aelias6004fe02015-02-07 21:43:0169 top_controls_shown_ratio_(top_controls_shown_ratio) {
[email protected]390bb1ff2014-05-09 17:14:4070}
[email protected]3b31c6ac2012-12-06 21:27:2971
72LayerTreeImpl::~LayerTreeImpl() {
[email protected]586871b2014-07-22 17:05:1173 BreakSwapPromises(SwapPromise::SWAP_FAILS);
74
[email protected]361bc00d2012-12-14 07:03:2475 // Need to explicitly clear the tree prior to destroying this so that
76 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
[email protected]df17af52014-02-06 02:20:4077 DCHECK(!root_layer_);
78 DCHECK(layers_with_copy_output_request_.empty());
[email protected]3b31c6ac2012-12-06 21:27:2979}
80
danakjf446a072014-09-27 21:55:4881void LayerTreeImpl::Shutdown() {
82 root_layer_ = nullptr;
83}
[email protected]df17af52014-02-06 02:20:4084
[email protected]aeef2f02014-05-10 12:15:4885void LayerTreeImpl::ReleaseResources() {
vmpstra73f18d2015-03-02 21:04:3986 if (root_layer_) {
87 LayerTreeHostCommon::CallFunctionForSubtree(
88 root_layer_.get(), [](LayerImpl* layer) { layer->ReleaseResources(); });
89 }
vmpstr9ce5c662015-02-05 23:29:2690}
91
92void LayerTreeImpl::RecreateResources() {
vmpstra73f18d2015-03-02 21:04:3993 if (root_layer_) {
94 LayerTreeHostCommon::CallFunctionForSubtree(
95 root_layer_.get(),
96 [](LayerImpl* layer) { layer->RecreateResources(); });
97 }
[email protected]aeef2f02014-05-10 12:15:4898}
99
vmpstrd704c872015-04-03 20:29:51100void LayerTreeImpl::GatherFrameTimingRequestIds(
101 std::vector<int64_t>* request_ids) {
102 if (!root_layer_)
103 return;
104
105 // TODO(vmpstr): Early out if there are no requests on any of the layers. For
106 // that, we need to inform LayerTreeImpl whenever there are requests when we
107 // get them.
108 LayerTreeHostCommon::CallFunctionForSubtree(
109 root_layer_.get(), [request_ids](LayerImpl* layer) {
110 layer->GatherFrameTimingRequestIds(request_ids);
111 });
112}
113
hush33370e12015-04-07 03:49:50114bool LayerTreeImpl::IsExternalFlingActive() const {
115 return root_layer_scroll_offset_delegate_ &&
116 root_layer_scroll_offset_delegate_->IsExternalFlingActive();
117}
[email protected]adeda572014-01-31 00:49:47118
hush33370e12015-04-07 03:49:50119void LayerTreeImpl::DidUpdateScrollOffset(int layer_id) {
120 int inner_layer_id = InnerViewportScrollLayer()
121 ? InnerViewportScrollLayer()->id()
122 : Layer::INVALID_ID;
123 int outer_layer_id = OuterViewportScrollLayer()
124 ? OuterViewportScrollLayer()->id()
125 : Layer::INVALID_ID;
126 if (layer_id != outer_layer_id && layer_id != inner_layer_id)
127 return;
128
129 if (!root_layer_scroll_offset_delegate_)
130 return;
131
132 UpdateRootScrollOffsetDelegate();
133}
134
135void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
[email protected]3b31c6ac2012-12-06 21:27:29136 root_layer_ = layer.Pass();
[email protected]5c4824e12013-01-12 16:34:53137 currently_scrolling_layer_ = NULL;
[email protected]adeda572014-01-31 00:49:47138 inner_viewport_scroll_layer_ = NULL;
139 outer_viewport_scroll_layer_ = NULL;
140 page_scale_layer_ = NULL;
[email protected]5c4824e12013-01-12 16:34:53141
[email protected]c1bb5af2013-03-13 19:06:27142 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]5c4824e12013-01-12 16:34:53143}
144
[email protected]adeda572014-01-31 00:49:47145LayerImpl* LayerTreeImpl::InnerViewportScrollLayer() const {
146 return inner_viewport_scroll_layer_;
147}
[email protected]3b31c6ac2012-12-06 21:27:29148
[email protected]adeda572014-01-31 00:49:47149LayerImpl* LayerTreeImpl::OuterViewportScrollLayer() const {
150 return outer_viewport_scroll_layer_;
151}
[email protected]1960a712013-04-30 17:06:47152
miletusf57925d2014-10-01 19:38:13153gfx::ScrollOffset LayerTreeImpl::TotalScrollOffset() const {
154 gfx::ScrollOffset offset;
[email protected]3b31c6ac2012-12-06 21:27:29155
[email protected]adeda572014-01-31 00:49:47156 if (inner_viewport_scroll_layer_)
aeliasd0070ba2015-01-31 13:44:49157 offset += inner_viewport_scroll_layer_->CurrentScrollOffset();
[email protected]adeda572014-01-31 00:49:47158
159 if (outer_viewport_scroll_layer_)
aeliasd0070ba2015-01-31 13:44:49160 offset += outer_viewport_scroll_layer_->CurrentScrollOffset();
[email protected]adeda572014-01-31 00:49:47161
162 return offset;
163}
164
miletusf57925d2014-10-01 19:38:13165gfx::ScrollOffset LayerTreeImpl::TotalMaxScrollOffset() const {
166 gfx::ScrollOffset offset;
[email protected]adeda572014-01-31 00:49:47167
168 if (inner_viewport_scroll_layer_)
169 offset += inner_viewport_scroll_layer_->MaxScrollOffset();
170
171 if (outer_viewport_scroll_layer_)
172 offset += outer_viewport_scroll_layer_->MaxScrollOffset();
173
174 return offset;
175}
[email protected]3b31c6ac2012-12-06 21:27:29176
177scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
178 // Clear all data structures that have direct references to the layer tree.
179 scrolling_layer_id_from_previous_tree_ =
180 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
[email protected]adeda572014-01-31 00:49:47181 inner_viewport_scroll_layer_ = NULL;
182 outer_viewport_scroll_layer_ = NULL;
183 page_scale_layer_ = NULL;
[email protected]3b31c6ac2012-12-06 21:27:29184 currently_scrolling_layer_ = NULL;
185
[email protected]76ffd9e2012-12-20 19:12:47186 render_surface_layer_list_.clear();
[email protected]615c78a2013-01-24 23:44:16187 set_needs_update_draw_properties();
[email protected]3b31c6ac2012-12-06 21:27:29188 return root_layer_.Pass();
189}
190
[email protected]7aba6662013-03-12 10:17:34191void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl* target_tree) {
[email protected]c9280762013-08-01 06:28:57192 // The request queue should have been processed and does not require a push.
193 DCHECK_EQ(ui_resource_request_queue_.size(), 0u);
194
ennee95b1542015-04-20 20:35:50195 target_tree->SetPropertyTrees(property_trees_);
196
[email protected]7d08a9352013-10-15 08:24:56197 if (next_activation_forces_redraw_) {
[email protected]12a63da2014-06-13 06:06:22198 target_tree->ForceRedrawNextActivation();
[email protected]7d08a9352013-10-15 08:24:56199 next_activation_forces_redraw_ = false;
200 }
201
[email protected]b69c1db2013-11-27 00:05:19202 target_tree->PassSwapPromises(&swap_promise_list_);
203
aelias6004fe02015-02-07 21:43:01204 target_tree->set_top_controls_shrink_blink_size(
205 top_controls_shrink_blink_size_);
206 target_tree->set_top_controls_height(top_controls_height_);
207 target_tree->PushTopControls(nullptr);
bokan88eae012014-09-09 20:40:42208
aelias58eec0812014-12-04 01:04:40209 // Active tree already shares the page_scale_factor object with pending
210 // tree so only the limits need to be provided.
211 target_tree->PushPageScaleFactorAndLimits(nullptr, min_page_scale_factor(),
212 max_page_scale_factor());
ccameronb9aec4502014-12-05 19:31:00213 target_tree->elastic_overscroll()->PushPendingToActive();
[email protected]c60279472013-01-30 12:10:51214
bokanfcdbc182014-11-21 21:53:33215 target_tree->pending_page_scale_animation_ =
216 pending_page_scale_animation_.Pass();
bokan915bf352014-10-02 21:57:14217
[email protected]adeda572014-01-31 00:49:47218 if (page_scale_layer_ && inner_viewport_scroll_layer_) {
[email protected]57ac9482013-09-17 21:13:39219 target_tree->SetViewportLayersFromIds(
ccameron8230b68b2014-11-21 19:25:18220 overscroll_elasticity_layer_ ? overscroll_elasticity_layer_->id()
221 : Layer::INVALID_ID,
222 page_scale_layer_->id(), inner_viewport_scroll_layer_->id(),
[email protected]57ac9482013-09-17 21:13:39223 outer_viewport_scroll_layer_ ? outer_viewport_scroll_layer_->id()
224 : Layer::INVALID_ID);
[email protected]adeda572014-01-31 00:49:47225 } else {
226 target_tree->ClearViewportLayers();
[email protected]57ac9482013-09-17 21:13:39227 }
[email protected]19aec372014-07-01 19:08:49228
jdduke449b5292015-04-23 19:36:44229 target_tree->RegisterSelection(selection_);
[email protected]19aec372014-07-01 19:08:49230
[email protected]c60279472013-01-30 12:10:51231 // This should match the property synchronization in
232 // LayerTreeHost::finishCommitOnImplThread().
233 target_tree->set_source_frame_number(source_frame_number());
234 target_tree->set_background_color(background_color());
235 target_tree->set_has_transparent_background(has_transparent_background());
236
237 if (ContentsTexturesPurged())
238 target_tree->SetContentsTexturesPurged();
239 else
240 target_tree->ResetContentsTexturesPurged();
241
[email protected]318822852013-02-14 00:54:27242 if (ViewportSizeInvalid())
243 target_tree->SetViewportSizeInvalid();
244 else
245 target_tree->ResetViewportSizeInvalid();
246
[email protected]c60279472013-01-30 12:10:51247 if (hud_layer())
248 target_tree->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl*>(
[email protected]6ba914122013-03-22 16:26:39249 LayerTreeHostCommon::FindLayerInSubtree(
[email protected]c1bb5af2013-03-13 19:06:27250 target_tree->root_layer(), hud_layer()->id())));
[email protected]c60279472013-01-30 12:10:51251 else
252 target_tree->set_hud_layer(NULL);
[email protected]759dc9f2014-07-23 19:18:51253
254 target_tree->has_ever_been_drawn_ = false;
[email protected]c60279472013-01-30 12:10:51255}
256
[email protected]fef74fd2014-02-27 06:28:17257LayerImpl* LayerTreeImpl::InnerViewportContainerLayer() const {
258 return inner_viewport_scroll_layer_
259 ? inner_viewport_scroll_layer_->scroll_clip_layer()
260 : NULL;
[email protected]ffb2720f2013-03-15 19:18:37261}
262
bokanef971462014-10-13 22:58:32263LayerImpl* LayerTreeImpl::OuterViewportContainerLayer() const {
264 return outer_viewport_scroll_layer_
265 ? outer_viewport_scroll_layer_->scroll_clip_layer()
266 : NULL;
267}
268
[email protected]ffb2720f2013-03-15 19:18:37269LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() const {
[email protected]69b50ec2013-01-19 04:58:01270 DCHECK(IsActiveTree());
271 return currently_scrolling_layer_;
272}
273
[email protected]0fc818e2013-03-18 06:45:20274void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl* layer) {
275 if (currently_scrolling_layer_ == layer)
276 return;
277
278 if (currently_scrolling_layer_ &&
279 currently_scrolling_layer_->scrollbar_animation_controller())
[email protected]1dc06162014-03-26 22:54:45280 currently_scrolling_layer_->scrollbar_animation_controller()
[email protected]930ff43b2014-05-02 05:24:00281 ->DidScrollEnd();
[email protected]0fc818e2013-03-18 06:45:20282 currently_scrolling_layer_ = layer;
283 if (layer && layer->scrollbar_animation_controller())
[email protected]930ff43b2014-05-02 05:24:00284 layer->scrollbar_animation_controller()->DidScrollBegin();
[email protected]0fc818e2013-03-18 06:45:20285}
286
[email protected]3b31c6ac2012-12-06 21:27:29287void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
[email protected]0fc818e2013-03-18 06:45:20288 SetCurrentlyScrollingLayer(NULL);
[email protected]3b31c6ac2012-12-06 21:27:29289 scrolling_layer_id_from_previous_tree_ = 0;
290}
291
[email protected]adeda572014-01-31 00:49:47292namespace {
293
294void ForceScrollbarParameterUpdateAfterScaleChange(LayerImpl* current_layer) {
295 if (!current_layer)
296 return;
297
298 while (current_layer) {
sataya.m07f11a82014-10-07 14:29:18299 current_layer->ScrollbarParametersDidChange(false);
[email protected]adeda572014-01-31 00:49:47300 current_layer = current_layer->parent();
301 }
302}
303
304} // namespace
305
aelias58eec0812014-12-04 01:04:40306float LayerTreeImpl::ClampPageScaleFactorToLimits(
307 float page_scale_factor) const {
308 if (min_page_scale_factor_ && page_scale_factor < min_page_scale_factor_)
309 page_scale_factor = min_page_scale_factor_;
310 else if (max_page_scale_factor_ && page_scale_factor > max_page_scale_factor_)
311 page_scale_factor = max_page_scale_factor_;
312 return page_scale_factor;
[email protected]d6021f6a2014-06-12 21:15:24313}
[email protected]c60279472013-01-30 12:10:51314
aelias58eec0812014-12-04 01:04:40315void LayerTreeImpl::SetPageScaleOnActiveTree(float active_page_scale) {
316 DCHECK(IsActiveTree());
317 if (page_scale_factor()->SetCurrent(
318 ClampPageScaleFactorToLimits(active_page_scale)))
319 DidUpdatePageScale();
[email protected]d6021f6a2014-06-12 21:15:24320}
321
aelias58eec0812014-12-04 01:04:40322void LayerTreeImpl::PushPageScaleFromMainThread(float page_scale_factor,
323 float min_page_scale_factor,
324 float max_page_scale_factor) {
325 PushPageScaleFactorAndLimits(&page_scale_factor, min_page_scale_factor,
326 max_page_scale_factor);
327}
328
329void LayerTreeImpl::PushPageScaleFactorAndLimits(const float* page_scale_factor,
330 float min_page_scale_factor,
331 float max_page_scale_factor) {
332 DCHECK(page_scale_factor || IsActiveTree());
333 bool changed_page_scale = false;
334 if (page_scale_factor) {
335 DCHECK(!IsActiveTree() || !layer_tree_host_impl_->pending_tree());
336 changed_page_scale |=
337 page_scale_factor_->PushFromMainThread(*page_scale_factor);
338 }
339 if (IsActiveTree())
340 changed_page_scale |= page_scale_factor_->PushPendingToActive();
341 changed_page_scale |=
342 SetPageScaleFactorLimits(min_page_scale_factor, max_page_scale_factor);
343
344 if (changed_page_scale)
345 DidUpdatePageScale();
346}
347
aelias6004fe02015-02-07 21:43:01348void LayerTreeImpl::set_top_controls_shrink_blink_size(bool shrink) {
349 if (top_controls_shrink_blink_size_ == shrink)
350 return;
351
352 top_controls_shrink_blink_size_ = shrink;
353 if (IsActiveTree())
354 layer_tree_host_impl_->UpdateViewportContainerSizes();
355}
356
357void LayerTreeImpl::set_top_controls_height(float top_controls_height) {
358 if (top_controls_height_ == top_controls_height)
359 return;
360
361 top_controls_height_ = top_controls_height;
362 if (IsActiveTree())
363 layer_tree_host_impl_->UpdateViewportContainerSizes();
364}
365
366bool LayerTreeImpl::SetCurrentTopControlsShownRatio(float ratio) {
367 ratio = std::max(ratio, 0.f);
368 ratio = std::min(ratio, 1.f);
369 return top_controls_shown_ratio_->SetCurrent(ratio);
370}
371
372void LayerTreeImpl::PushTopControlsFromMainThread(
373 float top_controls_shown_ratio) {
374 PushTopControls(&top_controls_shown_ratio);
375}
376
377void LayerTreeImpl::PushTopControls(const float* top_controls_shown_ratio) {
378 DCHECK(top_controls_shown_ratio || IsActiveTree());
379
380 if (top_controls_shown_ratio) {
381 DCHECK(!IsActiveTree() || !layer_tree_host_impl_->pending_tree());
382 top_controls_shown_ratio_->PushFromMainThread(*top_controls_shown_ratio);
383 }
384 if (IsActiveTree()) {
385 if (top_controls_shown_ratio_->PushPendingToActive())
386 layer_tree_host_impl_->DidChangeTopControlsPosition();
387 }
388}
389
aelias58eec0812014-12-04 01:04:40390bool LayerTreeImpl::SetPageScaleFactorLimits(float min_page_scale_factor,
391 float max_page_scale_factor) {
392 if (min_page_scale_factor == min_page_scale_factor_ &&
393 max_page_scale_factor == max_page_scale_factor_)
394 return false;
[email protected]7265e74e2014-02-07 23:43:06395
[email protected]c60279472013-01-30 12:10:51396 min_page_scale_factor_ = min_page_scale_factor;
397 max_page_scale_factor_ = max_page_scale_factor;
[email protected]20d2b742013-09-26 05:41:34398
aelias58eec0812014-12-04 01:04:40399 return true;
400}
[email protected]d6021f6a2014-06-12 21:15:24401
aelias58eec0812014-12-04 01:04:40402void LayerTreeImpl::DidUpdatePageScale() {
403 if (IsActiveTree())
404 page_scale_factor()->SetCurrent(
405 ClampPageScaleFactorToLimits(current_page_scale_factor()));
[email protected]d6021f6a2014-06-12 21:15:24406
aelias58eec0812014-12-04 01:04:40407 set_needs_update_draw_properties();
[email protected]d6021f6a2014-06-12 21:15:24408
[email protected]22f200a2013-10-09 18:08:29409 if (root_layer_scroll_offset_delegate_) {
[email protected]ec2322e2014-05-15 16:32:00410 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
aelias58eec0812014-12-04 01:04:40411 TotalScrollOffset(), TotalMaxScrollOffset(), ScrollableSize(),
412 current_page_scale_factor(), min_page_scale_factor_,
[email protected]d6021f6a2014-06-12 21:15:24413 max_page_scale_factor_);
[email protected]22f200a2013-10-09 18:08:29414 }
[email protected]adeda572014-01-31 00:49:47415
416 ForceScrollbarParameterUpdateAfterScaleChange(page_scale_layer());
bokanc784a6f2015-01-28 04:11:50417
418 HideInnerViewportScrollbarsIfNearMinimumScale();
419}
420
421void LayerTreeImpl::HideInnerViewportScrollbarsIfNearMinimumScale() {
422 if (!InnerViewportContainerLayer())
423 return;
424
425 LayerImpl::ScrollbarSet* scrollbars =
426 InnerViewportContainerLayer()->scrollbars();
427
428 if (!scrollbars)
429 return;
430
431 for (LayerImpl::ScrollbarSet::iterator it = scrollbars->begin();
432 it != scrollbars->end();
433 ++it) {
434 ScrollbarLayerImplBase* scrollbar = *it;
435 float minimum_scale_to_show_at =
436 min_page_scale_factor() * settings().scrollbar_show_scale_threshold;
437 scrollbar->SetHideLayerAndSubtree(
438 current_page_scale_factor() < minimum_scale_to_show_at);
439 }
[email protected]c60279472013-01-30 12:10:51440}
441
aelias58eec0812014-12-04 01:04:40442SyncedProperty<ScaleGroup>* LayerTreeImpl::page_scale_factor() {
443 return page_scale_factor_.get();
444}
445
446const SyncedProperty<ScaleGroup>* LayerTreeImpl::page_scale_factor() const {
447 return page_scale_factor_.get();
448}
449
[email protected]257abfa82013-01-29 23:47:24450gfx::SizeF LayerTreeImpl::ScrollableViewportSize() const {
[email protected]587941d2014-08-22 01:40:01451 if (!InnerViewportContainerLayer())
452 return gfx::SizeF();
453
bokanef971462014-10-13 22:58:32454 return gfx::ScaleSize(InnerViewportContainerLayer()->BoundsForScrolling(),
aelias58eec0812014-12-04 01:04:40455 1.0f / current_page_scale_factor());
[email protected]257abfa82013-01-29 23:47:24456}
457
[email protected]3744e27b2013-11-06 21:44:08458gfx::Rect LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
[email protected]adeda572014-01-31 00:49:47459 LayerImpl* root_scroll_layer = OuterViewportScrollLayer()
460 ? OuterViewportScrollLayer()
461 : InnerViewportScrollLayer();
462 if (!root_scroll_layer || root_scroll_layer->children().empty())
[email protected]3744e27b2013-11-06 21:44:08463 return gfx::Rect();
[email protected]adeda572014-01-31 00:49:47464 LayerImpl* layer = root_scroll_layer->children()[0];
[email protected]8a822692014-02-12 17:30:55465 return MathUtil::MapEnclosingClippedRect(layer->screen_space_transform(),
466 gfx::Rect(layer->content_bounds()));
[email protected]3744e27b2013-11-06 21:44:08467}
468
[email protected]58241dc2013-08-20 01:39:25469void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
[email protected]3519b872013-07-30 07:17:50470 DCHECK(IsActiveTree());
471
aelias58eec0812014-12-04 01:04:40472 page_scale_factor()->AbortCommit();
aelias6004fe02015-02-07 21:43:01473 top_controls_shown_ratio()->AbortCommit();
ccameronb9aec4502014-12-05 19:31:00474 elastic_overscroll()->AbortCommit();
[email protected]3519b872013-07-30 07:17:50475
476 if (!root_layer())
477 return;
478
479 LayerTreeHostCommon::CallFunctionForSubtree(
vmpstra73f18d2015-03-02 21:04:39480 root_layer(), [](LayerImpl* layer) {
481 layer->ApplySentScrollDeltasFromAbortedCommit();
482 });
[email protected]58241dc2013-08-20 01:39:25483}
484
[email protected]57ac9482013-09-17 21:13:39485void LayerTreeImpl::SetViewportLayersFromIds(
ccameron8230b68b2014-11-21 19:25:18486 int overscroll_elasticity_layer_id,
[email protected]57ac9482013-09-17 21:13:39487 int page_scale_layer_id,
488 int inner_viewport_scroll_layer_id,
489 int outer_viewport_scroll_layer_id) {
ccameron8230b68b2014-11-21 19:25:18490 overscroll_elasticity_layer_ = LayerById(overscroll_elasticity_layer_id);
[email protected]57ac9482013-09-17 21:13:39491 page_scale_layer_ = LayerById(page_scale_layer_id);
492 DCHECK(page_scale_layer_);
493
494 inner_viewport_scroll_layer_ =
495 LayerById(inner_viewport_scroll_layer_id);
496 DCHECK(inner_viewport_scroll_layer_);
497
498 outer_viewport_scroll_layer_ =
499 LayerById(outer_viewport_scroll_layer_id);
500 DCHECK(outer_viewport_scroll_layer_ ||
501 outer_viewport_scroll_layer_id == Layer::INVALID_ID);
[email protected]adeda572014-01-31 00:49:47502
bokanc784a6f2015-01-28 04:11:50503 HideInnerViewportScrollbarsIfNearMinimumScale();
[email protected]57ac9482013-09-17 21:13:39504}
505
506void LayerTreeImpl::ClearViewportLayers() {
507 page_scale_layer_ = NULL;
508 inner_viewport_scroll_layer_ = NULL;
509 outer_viewport_scroll_layer_ = NULL;
510}
511
enneaf5bda32015-02-19 01:27:36512bool LayerTreeImpl::UpdateDrawProperties(bool update_lcd_text) {
[email protected]8f7f298822014-06-13 00:23:32513 if (!needs_update_draw_properties_)
514 return true;
[email protected]615c78a2013-01-24 23:44:16515
enne0acea6ae2015-02-19 20:01:51516 // Calling UpdateDrawProperties must clear this flag, so there can be no
517 // early outs before this.
518 needs_update_draw_properties_ = false;
519
520 // For max_texture_size. When the renderer is re-created in
521 // CreateAndSetRenderer, the needs update draw properties flag is set
522 // again.
[email protected]615c78a2013-01-24 23:44:16523 if (!layer_tree_host_impl_->renderer())
[email protected]8f7f298822014-06-13 00:23:32524 return false;
[email protected]615c78a2013-01-24 23:44:16525
enne0acea6ae2015-02-19 20:01:51526 // Clear this after the renderer early out, as it should still be
527 // possible to hit test even without a renderer.
528 render_surface_layer_list_.clear();
529
[email protected]c1bb5af2013-03-13 19:06:27530 if (!root_layer())
[email protected]8f7f298822014-06-13 00:23:32531 return false;
532
[email protected]76ffd9e2012-12-20 19:12:47533 {
danakj4902c302015-02-13 22:12:16534 TRACE_EVENT2(
535 "cc", "LayerTreeImpl::UpdateDrawProperties::CalculateDrawProperties",
536 "IsActive", IsActiveTree(), "SourceFrameNumber", source_frame_number_);
[email protected]57ac9482013-09-17 21:13:39537 LayerImpl* page_scale_layer =
[email protected]fef74fd2014-02-27 06:28:17538 page_scale_layer_ ? page_scale_layer_ : InnerViewportContainerLayer();
hush367d7dd2014-08-29 23:56:01539 bool can_render_to_separate_surface =
540 (layer_tree_host_impl_->GetDrawMode() !=
541 DRAW_MODE_RESOURCELESS_SOFTWARE);
[email protected]390bb1ff2014-05-09 17:14:40542
543 ++render_surface_layer_list_id_;
ennec3011472015-04-09 01:27:25544
[email protected]7aad55f2013-07-26 11:25:53545 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
danakj3f76ace2014-11-18 16:56:00546 root_layer(), DrawViewportSize(),
547 layer_tree_host_impl_->DrawTransform(), device_scale_factor(),
aelias58eec0812014-12-04 01:04:40548 current_page_scale_factor(), page_scale_layer,
ccameronb9aec4502014-12-05 19:31:00549 elastic_overscroll()->Current(IsActiveTree()),
550 overscroll_elasticity_layer_, resource_provider()->max_texture_size(),
551 settings().can_use_lcd_text, settings().layers_always_allowed_lcd_text,
[email protected]45948712013-09-27 02:46:48552 can_render_to_separate_surface,
[email protected]35a99a12013-05-09 23:52:29553 settings().layer_transforms_should_scale_layer_contents,
ennec3011472015-04-09 01:27:25554 settings().verify_property_trees, &render_surface_layer_list_,
ennee95b1542015-04-20 20:35:50555 render_surface_layer_list_id_, &property_trees_);
[email protected]7aad55f2013-07-26 11:25:53556 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
[email protected]76ffd9e2012-12-20 19:12:47557 }
[email protected]615c78a2013-01-24 23:44:16558
[email protected]e4be0262013-10-19 16:54:28559 {
danakj4902c302015-02-13 22:12:16560 TRACE_EVENT2("cc", "LayerTreeImpl::UpdateDrawProperties::Occlusion",
561 "IsActive", IsActiveTree(), "SourceFrameNumber",
562 source_frame_number_);
563 OcclusionTracker<LayerImpl> occlusion_tracker(
564 root_layer()->render_surface()->content_rect());
565 occlusion_tracker.set_minimum_tracking_size(
566 settings().minimum_occlusion_tracking_size);
boliu7473f7f52014-10-01 16:54:56567
[email protected]e4be0262013-10-19 16:54:28568 // LayerIterator is used here instead of CallFunctionForSubtree to only
569 // UpdateTilePriorities on layers that will be visible (and thus have valid
570 // draw properties) and not because any ordering is required.
danakj4902c302015-02-13 22:12:16571 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list_);
572 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list_);
573 it != end; ++it) {
574 occlusion_tracker.EnterLayer(it);
[email protected]562b7ad2014-06-23 22:17:11575
danakj4902c302015-02-13 22:12:16576 // There are very few render targets so this should be cheap to do for
577 // each layer instead of something more complicated.
578 bool inside_replica = false;
579 LayerImpl* layer = it->render_target();
580 while (layer && !inside_replica) {
581 if (layer->render_target()->has_replica())
582 inside_replica = true;
583 layer = layer->render_target()->parent();
584 }
585
586 // Don't use occlusion if a layer will appear in a replica, since the
587 // tile raster code does not know how to look for the replica and would
588 // consider it occluded even though the replica is visible.
589 // Since occlusion is only used for browser compositor (i.e.
590 // use_occlusion_for_tile_prioritization) and it won't use replicas,
591 // this should matter not.
vmpstrcdcb5f72014-09-11 00:58:37592
boliu7473f7f52014-10-01 16:54:56593 if (it.represents_itself()) {
danakj4902c302015-02-13 22:12:16594 Occlusion occlusion =
595 inside_replica ? Occlusion()
596 : occlusion_tracker.GetCurrentOcclusionForLayer(
597 it->draw_transform());
598 it->draw_properties().occlusion_in_content_space = occlusion;
boliu7473f7f52014-10-01 16:54:56599 }
[email protected]e4be0262013-10-19 16:54:28600
danakj4902c302015-02-13 22:12:16601 if (it.represents_contributing_render_surface()) {
602 // Surfaces aren't used by the tile raster code, so they can have
603 // occlusion regardless of replicas.
604 Occlusion occlusion =
605 occlusion_tracker.GetCurrentOcclusionForContributingSurface(
606 it->render_surface()->draw_transform());
607 it->render_surface()->set_occlusion_in_content_space(occlusion);
608 // Masks are used to draw the contributing surface, so should have
609 // the same occlusion as the surface (nothing inside the surface
610 // occludes them).
611 if (LayerImpl* mask = it->mask_layer()) {
612 Occlusion mask_occlusion =
613 inside_replica
614 ? Occlusion()
615 : occlusion_tracker.GetCurrentOcclusionForContributingSurface(
616 it->render_surface()->draw_transform() *
617 it->draw_transform());
618 mask->draw_properties().occlusion_in_content_space = mask_occlusion;
619 }
620 if (LayerImpl* replica = it->replica_layer()) {
621 if (LayerImpl* mask = replica->mask_layer())
622 mask->draw_properties().occlusion_in_content_space = Occlusion();
623 }
624 }
625
626 occlusion_tracker.LeaveLayer(it);
627 }
628
629 unoccluded_screen_space_region_ =
630 occlusion_tracker.ComputeVisibleRegionInScreen();
631 }
632
enneaf5bda32015-02-19 01:27:36633 // It'd be ideal if this could be done earlier, but when the raster source
634 // is updated from the main thread during push properties, update draw
635 // properties has not occurred yet and so it's not clear whether or not the
636 // layer can or cannot use lcd text. So, this is the cleanup pass to
637 // determine if the raster source needs to be replaced with a non-lcd
638 // raster source due to draw properties.
639 if (update_lcd_text) {
640 // TODO(enne): Make LTHI::sync_tree return this value.
641 LayerTreeImpl* sync_tree =
642 layer_tree_host_impl_->proxy()->CommitToActiveTree()
643 ? layer_tree_host_impl_->active_tree()
644 : layer_tree_host_impl_->pending_tree();
645 // If this is not the sync tree, then it is not safe to update lcd text
646 // as it causes invalidations and the tiles may be in use.
647 DCHECK_EQ(this, sync_tree);
648 for (const auto& layer : picture_layers_)
649 layer->UpdateCanUseLCDTextAfterCommit();
650 }
651
danakj4902c302015-02-13 22:12:16652 {
653 TRACE_EVENT_BEGIN2("cc", "LayerTreeImpl::UpdateDrawProperties::UpdateTiles",
654 "IsActive", IsActiveTree(), "SourceFrameNumber",
655 source_frame_number_);
656 const bool resourceless_software_draw =
657 (layer_tree_host_impl_->GetDrawMode() ==
658 DRAW_MODE_RESOURCELESS_SOFTWARE);
danakj4902c302015-02-13 22:12:16659 size_t layers_updated_count = 0;
660 bool tile_priorities_updated = false;
661 for (PictureLayerImpl* layer : picture_layers_) {
danakj4902c302015-02-13 22:12:16662 if (!layer->IsDrawnRenderSurfaceLayerListMember())
[email protected]6355d2d2014-05-07 15:07:27663 continue;
danakj4902c302015-02-13 22:12:16664 ++layers_updated_count;
danakj83c3d4a82015-02-14 01:42:57665 tile_priorities_updated |= layer->UpdateTiles(resourceless_software_draw);
[email protected]e4be0262013-10-19 16:54:28666 }
vmpstrd6166202014-11-05 18:45:40667
vmpstr5377520a2014-12-29 23:26:13668 if (tile_priorities_updated)
669 DidModifyTilePriorities();
670
vmpstrd6166202014-11-05 18:45:40671 TRACE_EVENT_END1("cc", "LayerTreeImpl::UpdateTilePriorities",
672 "layers_updated_count", layers_updated_count);
[email protected]e4be0262013-10-19 16:54:28673 }
674
[email protected]615c78a2013-01-24 23:44:16675 DCHECK(!needs_update_draw_properties_) <<
[email protected]7d19dc342013-05-02 22:02:04676 "CalcDrawProperties should not set_needs_update_draw_properties()";
[email protected]8f7f298822014-06-13 00:23:32677 return true;
[email protected]76ffd9e2012-12-20 19:12:47678}
679
[email protected]50761e92013-03-29 20:51:28680const LayerImplList& LayerTreeImpl::RenderSurfaceLayerList() const {
[email protected]76ffd9e2012-12-20 19:12:47681 // If this assert triggers, then the list is dirty.
[email protected]615c78a2013-01-24 23:44:16682 DCHECK(!needs_update_draw_properties_);
[email protected]76ffd9e2012-12-20 19:12:47683 return render_surface_layer_list_;
684}
685
danakj4902c302015-02-13 22:12:16686const Region& LayerTreeImpl::UnoccludedScreenSpaceRegion() const {
687 // If this assert triggers, then the render_surface_layer_list_ is dirty, so
688 // the unoccluded_screen_space_region_ is not valid anymore.
689 DCHECK(!needs_update_draw_properties_);
690 return unoccluded_screen_space_region_;
691}
692
bokancccfde72014-10-08 15:15:22693gfx::Size LayerTreeImpl::ScrollableSize() const {
[email protected]adeda572014-01-31 00:49:47694 LayerImpl* root_scroll_layer = OuterViewportScrollLayer()
695 ? OuterViewportScrollLayer()
696 : InnerViewportScrollLayer();
697 if (!root_scroll_layer || root_scroll_layer->children().empty())
bokancccfde72014-10-08 15:15:22698 return gfx::Size();
[email protected]adeda572014-01-31 00:49:47699 return root_scroll_layer->children()[0]->bounds();
[email protected]caa567d2012-12-20 07:56:16700}
701
[email protected]361bc00d2012-12-14 07:03:24702LayerImpl* LayerTreeImpl::LayerById(int id) {
703 LayerIdMap::iterator iter = layer_id_map_.find(id);
704 return iter != layer_id_map_.end() ? iter->second : NULL;
705}
706
707void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
708 DCHECK(!LayerById(layer->id()));
709 layer_id_map_[layer->id()] = layer;
710}
711
712void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
713 DCHECK(LayerById(layer->id()));
714 layer_id_map_.erase(layer->id());
715}
716
[email protected]aebf4622014-07-14 16:57:59717size_t LayerTreeImpl::NumLayers() {
718 return layer_id_map_.size();
719}
720
[email protected]ed511b8d2013-03-25 03:29:29721void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pending_tree) {
[email protected]a90fac72013-06-06 18:56:13722 pending_tree->SetCurrentlyScrollingLayer(
723 LayerTreeHostCommon::FindLayerInSubtree(pending_tree->root_layer(),
724 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0));
[email protected]1e0f8d62013-01-09 07:41:35725}
726
[email protected]37386f052013-01-13 00:42:22727void LayerTreeImpl::DidBecomeActive() {
[email protected]12a63da2014-06-13 06:06:22728 if (next_activation_forces_redraw_) {
729 layer_tree_host_impl_->SetFullRootLayerDamage();
730 next_activation_forces_redraw_ = false;
731 }
732
[email protected]adeda572014-01-31 00:49:47733 if (scrolling_layer_id_from_previous_tree_) {
734 currently_scrolling_layer_ = LayerTreeHostCommon::FindLayerInSubtree(
[email protected]7dcf5632014-06-25 01:11:55735 root_layer(), scrolling_layer_id_from_previous_tree_);
[email protected]adeda572014-01-31 00:49:47736 }
737
[email protected]7dcf5632014-06-25 01:11:55738 // Always reset this flag on activation, as we would only have activated
739 // if we were in a good state.
vmpstr61ed94a12014-10-09 04:49:30740 layer_tree_host_impl_->ResetRequiresHighResToDraw();
[email protected]7dcf5632014-06-25 01:11:55741
vmpstra73f18d2015-03-02 21:04:39742 if (root_layer()) {
743 LayerTreeHostCommon::CallFunctionForSubtree(
744 root_layer(), [](LayerImpl* layer) { layer->DidBecomeActive(); });
745 }
[email protected]7dcf5632014-06-25 01:11:55746
[email protected]12a63da2014-06-13 06:06:22747 devtools_instrumentation::DidActivateLayerTree(layer_tree_host_impl_->id(),
748 source_frame_number_);
[email protected]37386f052013-01-13 00:42:22749}
750
[email protected]6f90b9e2013-01-17 23:42:00751bool LayerTreeImpl::ContentsTexturesPurged() const {
752 return contents_textures_purged_;
753}
754
755void LayerTreeImpl::SetContentsTexturesPurged() {
[email protected]94bf75c2013-06-12 13:20:04756 if (contents_textures_purged_)
757 return;
[email protected]6f90b9e2013-01-17 23:42:00758 contents_textures_purged_ = true;
[email protected]c1bb5af2013-03-13 19:06:27759 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]6f90b9e2013-01-17 23:42:00760}
761
762void LayerTreeImpl::ResetContentsTexturesPurged() {
[email protected]94bf75c2013-06-12 13:20:04763 if (!contents_textures_purged_)
764 return;
[email protected]6f90b9e2013-01-17 23:42:00765 contents_textures_purged_ = false;
[email protected]c1bb5af2013-03-13 19:06:27766 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]6f90b9e2013-01-17 23:42:00767}
768
[email protected]3d609bb2014-02-01 01:10:23769bool LayerTreeImpl::RequiresHighResToDraw() const {
vmpstr61ed94a12014-10-09 04:49:30770 return layer_tree_host_impl_->RequiresHighResToDraw();
[email protected]3d609bb2014-02-01 01:10:23771}
772
[email protected]318822852013-02-14 00:54:27773bool LayerTreeImpl::ViewportSizeInvalid() const {
774 return viewport_size_invalid_;
775}
776
777void LayerTreeImpl::SetViewportSizeInvalid() {
778 viewport_size_invalid_ = true;
[email protected]c1bb5af2013-03-13 19:06:27779 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]318822852013-02-14 00:54:27780}
781
782void LayerTreeImpl::ResetViewportSizeInvalid() {
783 viewport_size_invalid_ = false;
[email protected]c1bb5af2013-03-13 19:06:27784 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]318822852013-02-14 00:54:27785}
786
[email protected]48871fc2013-01-23 07:36:51787Proxy* LayerTreeImpl::proxy() const {
788 return layer_tree_host_impl_->proxy();
789}
790
[email protected]ff762fb2012-12-12 19:18:37791const LayerTreeSettings& LayerTreeImpl::settings() const {
[email protected]c1bb5af2013-03-13 19:06:27792 return layer_tree_host_impl_->settings();
[email protected]ff762fb2012-12-12 19:18:37793}
794
danakj875efa42015-02-10 22:18:23795const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
796 return layer_tree_host_impl_->debug_state();
797}
798
[email protected]7a8bcd262014-01-15 12:54:58799const RendererCapabilitiesImpl& LayerTreeImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:27800 return layer_tree_host_impl_->GetRendererCapabilities();
[email protected]bf5b3a02013-02-13 02:02:52801}
802
[email protected]0634cdd42013-08-16 00:46:09803ContextProvider* LayerTreeImpl::context_provider() const {
dcheng6afa17002014-08-26 19:11:31804 return output_surface()->context_provider();
[email protected]0634cdd42013-08-16 00:46:09805}
806
[email protected]ff762fb2012-12-12 19:18:37807OutputSurface* LayerTreeImpl::output_surface() const {
[email protected]c1bb5af2013-03-13 19:06:27808 return layer_tree_host_impl_->output_surface();
[email protected]ff762fb2012-12-12 19:18:37809}
810
811ResourceProvider* LayerTreeImpl::resource_provider() const {
[email protected]c1bb5af2013-03-13 19:06:27812 return layer_tree_host_impl_->resource_provider();
[email protected]ff762fb2012-12-12 19:18:37813}
814
815TileManager* LayerTreeImpl::tile_manager() const {
[email protected]c1bb5af2013-03-13 19:06:27816 return layer_tree_host_impl_->tile_manager();
[email protected]ff762fb2012-12-12 19:18:37817}
818
819FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
[email protected]c1bb5af2013-03-13 19:06:27820 return layer_tree_host_impl_->fps_counter();
[email protected]ff762fb2012-12-12 19:18:37821}
822
[email protected]71691c22013-01-18 03:14:22823PaintTimeCounter* LayerTreeImpl::paint_time_counter() const {
[email protected]c1bb5af2013-03-13 19:06:27824 return layer_tree_host_impl_->paint_time_counter();
[email protected]71691c22013-01-18 03:14:22825}
826
[email protected]1191d9d2013-02-02 06:00:33827MemoryHistory* LayerTreeImpl::memory_history() const {
[email protected]c1bb5af2013-03-13 19:06:27828 return layer_tree_host_impl_->memory_history();
[email protected]1191d9d2013-02-02 06:00:33829}
830
[email protected]4a6c091d2014-04-24 21:06:46831gfx::Size LayerTreeImpl::device_viewport_size() const {
832 return layer_tree_host_impl_->device_viewport_size();
833}
834
danakj875efa42015-02-10 22:18:23835float LayerTreeImpl::device_scale_factor() const {
836 return layer_tree_host_impl_->device_scale_factor();
837}
838
839DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
840 return layer_tree_host_impl_->debug_rect_history();
841}
842
[email protected]f117a4c2012-12-16 04:53:10843bool LayerTreeImpl::IsActiveTree() const {
[email protected]c1bb5af2013-03-13 19:06:27844 return layer_tree_host_impl_->active_tree() == this;
[email protected]f117a4c2012-12-16 04:53:10845}
846
847bool LayerTreeImpl::IsPendingTree() const {
[email protected]c1bb5af2013-03-13 19:06:27848 return layer_tree_host_impl_->pending_tree() == this;
[email protected]f117a4c2012-12-16 04:53:10849}
850
[email protected]48871fc2013-01-23 07:36:51851bool LayerTreeImpl::IsRecycleTree() const {
[email protected]c1bb5af2013-03-13 19:06:27852 return layer_tree_host_impl_->recycle_tree() == this;
[email protected]48871fc2013-01-23 07:36:51853}
854
enneaf5bda32015-02-19 01:27:36855bool LayerTreeImpl::IsSyncTree() const {
856 return layer_tree_host_impl_->sync_tree() == this;
857}
858
[email protected]f117a4c2012-12-16 04:53:10859LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
[email protected]c1bb5af2013-03-13 19:06:27860 LayerTreeImpl* tree = layer_tree_host_impl_->active_tree();
[email protected]f117a4c2012-12-16 04:53:10861 if (!tree)
862 return NULL;
863 return tree->LayerById(id);
864}
865
866LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
[email protected]c1bb5af2013-03-13 19:06:27867 LayerTreeImpl* tree = layer_tree_host_impl_->pending_tree();
[email protected]f117a4c2012-12-16 04:53:10868 if (!tree)
869 return NULL;
870 return tree->LayerById(id);
871}
872
[email protected]166db5c82013-01-09 23:54:31873bool LayerTreeImpl::PinchGestureActive() const {
[email protected]c1bb5af2013-03-13 19:06:27874 return layer_tree_host_impl_->pinch_gesture_active();
[email protected]166db5c82013-01-09 23:54:31875}
876
[email protected]04c5900d2014-08-18 13:38:36877BeginFrameArgs LayerTreeImpl::CurrentBeginFrameArgs() const {
878 return layer_tree_host_impl_->CurrentBeginFrameArgs();
[email protected]fb7425a2013-04-22 16:28:55879}
880
[email protected]c92195e2014-05-07 18:18:49881base::TimeDelta LayerTreeImpl::begin_impl_frame_interval() const {
882 return layer_tree_host_impl_->begin_impl_frame_interval();
883}
884
[email protected]d7eb8c72013-03-23 22:57:13885void LayerTreeImpl::SetNeedsCommit() {
886 layer_tree_host_impl_->SetNeedsCommit();
887}
888
[email protected]bd5324592014-07-31 09:09:33889gfx::Rect LayerTreeImpl::DeviceViewport() const {
890 return layer_tree_host_impl_->DeviceViewport();
891}
892
[email protected]54af03522013-09-05 00:43:28893gfx::Size LayerTreeImpl::DrawViewportSize() const {
894 return layer_tree_host_impl_->DrawViewportSize();
895}
896
[email protected]bd5324592014-07-31 09:09:33897const gfx::Rect LayerTreeImpl::ViewportRectForTilePriority() const {
898 return layer_tree_host_impl_->ViewportRectForTilePriority();
899}
900
[email protected]930ff43b2014-05-02 05:24:00901scoped_ptr<ScrollbarAnimationController>
902LayerTreeImpl::CreateScrollbarAnimationController(LayerImpl* scrolling_layer) {
903 DCHECK(settings().scrollbar_fade_delay_ms);
904 DCHECK(settings().scrollbar_fade_duration_ms);
905 base::TimeDelta delay =
906 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_delay_ms);
sataya.m07f11a82014-10-07 14:29:18907 base::TimeDelta resize_delay = base::TimeDelta::FromMilliseconds(
908 settings().scrollbar_fade_resize_delay_ms);
[email protected]930ff43b2014-05-02 05:24:00909 base::TimeDelta duration =
910 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_duration_ms);
911 switch (settings().scrollbar_animator) {
ericrk7c030992015-02-20 01:39:38912 case LayerTreeSettings::LINEAR_FADE: {
[email protected]930ff43b2014-05-02 05:24:00913 return ScrollbarAnimationControllerLinearFade::Create(
sataya.m07f11a82014-10-07 14:29:18914 scrolling_layer,
915 layer_tree_host_impl_,
916 delay,
917 resize_delay,
918 duration);
[email protected]930ff43b2014-05-02 05:24:00919 }
ericrk7c030992015-02-20 01:39:38920 case LayerTreeSettings::THINNING: {
sataya.m07f11a82014-10-07 14:29:18921 return ScrollbarAnimationControllerThinning::Create(scrolling_layer,
922 layer_tree_host_impl_,
923 delay,
924 resize_delay,
925 duration);
[email protected]930ff43b2014-05-02 05:24:00926 }
ericrk7c030992015-02-20 01:39:38927 case LayerTreeSettings::NO_ANIMATOR:
[email protected]930ff43b2014-05-02 05:24:00928 NOTREACHED();
929 break;
930 }
danakjf446a072014-09-27 21:55:48931 return nullptr;
[email protected]2ea5aba2013-09-11 14:26:56932}
933
[email protected]b8384e22013-12-03 02:20:48934void LayerTreeImpl::DidAnimateScrollOffset() {
935 layer_tree_host_impl_->DidAnimateScrollOffset();
936}
937
[email protected]13525d62014-05-20 21:22:04938bool LayerTreeImpl::use_gpu_rasterization() const {
939 return layer_tree_host_impl_->use_gpu_rasterization();
940}
941
hendrikwc2bbd612014-12-03 23:49:34942GpuRasterizationStatus LayerTreeImpl::GetGpuRasterizationStatus() const {
943 return layer_tree_host_impl_->gpu_rasterization_status();
944}
945
[email protected]473f1f22014-05-22 08:19:17946bool LayerTreeImpl::create_low_res_tiling() const {
947 return layer_tree_host_impl_->create_low_res_tiling();
948}
949
[email protected]ff762fb2012-12-12 19:18:37950void LayerTreeImpl::SetNeedsRedraw() {
[email protected]59adb112013-04-09 04:48:44951 layer_tree_host_impl_->SetNeedsRedraw();
[email protected]ff762fb2012-12-12 19:18:37952}
953
sunnypsea328be12015-02-26 06:03:27954AnimationRegistrar* LayerTreeImpl::GetAnimationRegistrar() const {
[email protected]c1bb5af2013-03-13 19:06:27955 return layer_tree_host_impl_->animation_registrar();
[email protected]de4afb5e2012-12-20 00:11:34956}
[email protected]ff762fb2012-12-12 19:18:37957
vmpstrdcf21da2015-03-23 19:16:32958void LayerTreeImpl::GetAllTilesAndPrioritiesForTracing(
959 std::map<const Tile*, TilePriority>* tile_map) const {
vmpstrd7de03c2014-08-27 18:11:01960 typedef LayerIterator<LayerImpl> LayerIteratorType;
961 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
962 for (LayerIteratorType it =
963 LayerIteratorType::Begin(&render_surface_layer_list_);
964 it != end;
965 ++it) {
966 if (!it.represents_itself())
967 continue;
968 LayerImpl* layer_impl = *it;
vmpstrdcf21da2015-03-23 19:16:32969 layer_impl->GetAllTilesAndPrioritiesForTracing(tile_map);
vmpstrd7de03c2014-08-27 18:11:01970 }
971}
972
ssid911e40e2015-02-09 17:55:20973void LayerTreeImpl::AsValueInto(base::trace_event::TracedValue* state) const {
[email protected]d12aa932014-08-01 13:10:38974 TracedValue::MakeDictIntoImplicitSnapshot(state, "cc::LayerTreeImpl", this);
nduca929378a02014-08-23 19:48:52975 state->SetInteger("source_frame_number", source_frame_number_);
[email protected]f6742f52013-05-08 23:52:22976
[email protected]d12aa932014-08-01 13:10:38977 state->BeginDictionary("root_layer");
978 root_layer_->AsValueInto(state);
979 state->EndDictionary();
[email protected]f6742f52013-05-08 23:52:22980
[email protected]d12aa932014-08-01 13:10:38981 state->BeginArray("render_surface_layer_list");
[email protected]ba1b33e2014-02-28 16:44:51982 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]71dfcc72013-03-20 21:30:09983 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
984 for (LayerIteratorType it = LayerIteratorType::Begin(
[email protected]8c5690222013-02-15 17:36:43985 &render_surface_layer_list_); it != end; ++it) {
[email protected]71dfcc72013-03-20 21:30:09986 if (!it.represents_itself())
[email protected]8c5690222013-02-15 17:36:43987 continue;
[email protected]d12aa932014-08-01 13:10:38988 TracedValue::AppendIDRef(*it, state);
[email protected]8c5690222013-02-15 17:36:43989 }
[email protected]d12aa932014-08-01 13:10:38990 state->EndArray();
skyostil43c330f72014-09-22 16:49:11991
992 state->BeginArray("swap_promise_trace_ids");
993 for (size_t i = 0; i < swap_promise_list_.size(); i++)
994 state->AppendDouble(swap_promise_list_[i]->TraceId());
995 state->EndArray();
[email protected]8c5690222013-02-15 17:36:43996}
997
[email protected]1960a712013-04-30 17:06:47998void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
[email protected]c9280762013-08-01 06:28:57999 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
[email protected]20d2b742013-09-26 05:41:341000 if (root_layer_scroll_offset_delegate_ == root_layer_scroll_offset_delegate)
1001 return;
1002
[email protected]adeda572014-01-31 00:49:471003 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
1004
[email protected]20d2b742013-09-26 05:41:341005 if (root_layer_scroll_offset_delegate_) {
[email protected]ec2322e2014-05-15 16:32:001006 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
aelias58eec0812014-12-04 01:04:401007 TotalScrollOffset(), TotalMaxScrollOffset(), ScrollableSize(),
1008 current_page_scale_factor(), min_page_scale_factor(),
[email protected]68fe60f2014-02-12 13:49:111009 max_page_scale_factor());
[email protected]adeda572014-01-31 00:49:471010
hush33370e12015-04-07 03:49:501011 DistributeRootScrollOffset();
[email protected]20d2b742013-09-26 05:41:341012 }
[email protected]1960a712013-04-30 17:06:471013}
1014
hush33370e12015-04-07 03:49:501015void LayerTreeImpl::UpdateRootScrollOffsetDelegate() {
[email protected]adeda572014-01-31 00:49:471016 DCHECK(root_layer_scroll_offset_delegate_);
1017
hush33370e12015-04-07 03:49:501018 gfx::ScrollOffset offset = InnerViewportScrollLayer()->CurrentScrollOffset();
[email protected]adeda572014-01-31 00:49:471019
1020 if (OuterViewportScrollLayer())
hush33370e12015-04-07 03:49:501021 offset += OuterViewportScrollLayer()->CurrentScrollOffset();
[email protected]adeda572014-01-31 00:49:471022
[email protected]ec2322e2014-05-15 16:32:001023 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
aelias58eec0812014-12-04 01:04:401024 offset, TotalMaxScrollOffset(), ScrollableSize(),
1025 current_page_scale_factor(), min_page_scale_factor(),
[email protected]ec2322e2014-05-15 16:32:001026 max_page_scale_factor());
[email protected]adeda572014-01-31 00:49:471027}
1028
hush33370e12015-04-07 03:49:501029void LayerTreeImpl::DistributeRootScrollOffset() {
1030 if (!root_layer_scroll_offset_delegate_)
1031 return;
1032
1033 gfx::ScrollOffset root_offset =
1034 root_layer_scroll_offset_delegate_->GetTotalScrollOffset();
1035
1036 if (!InnerViewportScrollLayer())
1037 return;
1038
1039 DCHECK(OuterViewportScrollLayer());
[email protected]adeda572014-01-31 00:49:471040
1041 // If we get here, we have both inner/outer viewports, and need to distribute
1042 // the scroll offset between them.
miletusf57925d2014-10-01 19:38:131043 gfx::ScrollOffset inner_viewport_offset =
hush33370e12015-04-07 03:49:501044 InnerViewportScrollLayer()->CurrentScrollOffset();
miletusf57925d2014-10-01 19:38:131045 gfx::ScrollOffset outer_viewport_offset =
hush33370e12015-04-07 03:49:501046 OuterViewportScrollLayer()->CurrentScrollOffset();
[email protected]adeda572014-01-31 00:49:471047
1048 // It may be nothing has changed.
hush33370e12015-04-07 03:49:501049 if (inner_viewport_offset + outer_viewport_offset == root_offset)
1050 return;
[email protected]adeda572014-01-31 00:49:471051
miletusf57925d2014-10-01 19:38:131052 gfx::ScrollOffset max_outer_viewport_scroll_offset =
[email protected]adeda572014-01-31 00:49:471053 OuterViewportScrollLayer()->MaxScrollOffset();
1054
hush33370e12015-04-07 03:49:501055 outer_viewport_offset = root_offset - inner_viewport_offset;
[email protected]adeda572014-01-31 00:49:471056 outer_viewport_offset.SetToMin(max_outer_viewport_scroll_offset);
miletusf57925d2014-10-01 19:38:131057 outer_viewport_offset.SetToMax(gfx::ScrollOffset());
[email protected]adeda572014-01-31 00:49:471058
hush33370e12015-04-07 03:49:501059 OuterViewportScrollLayer()->SetCurrentScrollOffsetFromDelegate(
1060 outer_viewport_offset);
1061 inner_viewport_offset = root_offset - outer_viewport_offset;
1062 InnerViewportScrollLayer()->SetCurrentScrollOffsetFromDelegate(
1063 inner_viewport_offset);
[email protected]adeda572014-01-31 00:49:471064
hush33370e12015-04-07 03:49:501065 UpdateRootScrollOffsetDelegate();
[email protected]adeda572014-01-31 00:49:471066}
1067
[email protected]b69c1db2013-11-27 00:05:191068void LayerTreeImpl::QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise) {
1069 DCHECK(swap_promise);
[email protected]b69c1db2013-11-27 00:05:191070 swap_promise_list_.push_back(swap_promise.Pass());
1071}
1072
1073void LayerTreeImpl::PassSwapPromises(
1074 ScopedPtrVector<SwapPromise>* new_swap_promise) {
1075 swap_promise_list_.insert_and_take(swap_promise_list_.end(),
weiliangcc1878c62014-09-02 22:43:171076 new_swap_promise);
[email protected]b69c1db2013-11-27 00:05:191077 new_swap_promise->clear();
1078}
1079
[email protected]d359203a2013-11-29 06:16:551080void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata* metadata) {
[email protected]b69c1db2013-11-27 00:05:191081 for (size_t i = 0; i < swap_promise_list_.size(); i++)
[email protected]d359203a2013-11-29 06:16:551082 swap_promise_list_[i]->DidSwap(metadata);
[email protected]b69c1db2013-11-27 00:05:191083 swap_promise_list_.clear();
1084}
1085
1086void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason) {
1087 for (size_t i = 0; i < swap_promise_list_.size(); i++)
1088 swap_promise_list_[i]->DidNotSwap(reason);
1089 swap_promise_list_.clear();
1090}
1091
[email protected]c48536a52013-09-14 00:02:081092void LayerTreeImpl::DidModifyTilePriorities() {
1093 layer_tree_host_impl_->DidModifyTilePriorities();
[email protected]fcb846d2013-05-22 01:42:361094}
1095
[email protected]c9280762013-08-01 06:28:571096void LayerTreeImpl::set_ui_resource_request_queue(
1097 const UIResourceRequestQueue& queue) {
1098 ui_resource_request_queue_ = queue;
1099}
1100
1101ResourceProvider::ResourceId LayerTreeImpl::ResourceIdForUIResource(
1102 UIResourceId uid) const {
1103 return layer_tree_host_impl_->ResourceIdForUIResource(uid);
1104}
1105
[email protected]709c9542013-10-26 01:43:511106bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid) const {
1107 return layer_tree_host_impl_->IsUIResourceOpaque(uid);
1108}
1109
[email protected]c9280762013-08-01 06:28:571110void LayerTreeImpl::ProcessUIResourceRequestQueue() {
jdduke5ad36812015-01-02 17:59:321111 for (const auto& req : ui_resource_request_queue_) {
[email protected]741fba422013-09-20 03:34:141112 switch (req.GetType()) {
ericrk7c030992015-02-20 01:39:381113 case UIResourceRequest::UI_RESOURCE_CREATE:
[email protected]741fba422013-09-20 03:34:141114 layer_tree_host_impl_->CreateUIResource(req.GetId(), req.GetBitmap());
[email protected]c9280762013-08-01 06:28:571115 break;
ericrk7c030992015-02-20 01:39:381116 case UIResourceRequest::UI_RESOURCE_DELETE:
[email protected]741fba422013-09-20 03:34:141117 layer_tree_host_impl_->DeleteUIResource(req.GetId());
[email protected]c9280762013-08-01 06:28:571118 break;
ericrk7c030992015-02-20 01:39:381119 case UIResourceRequest::UI_RESOURCE_INVALID_REQUEST:
[email protected]c9280762013-08-01 06:28:571120 NOTREACHED();
1121 break;
1122 }
1123 }
jdduke5ad36812015-01-02 17:59:321124 ui_resource_request_queue_.clear();
[email protected]127bdc1a2013-09-11 01:44:481125
1126 // If all UI resource evictions were not recreated by processing this queue,
1127 // then another commit is required.
1128 if (layer_tree_host_impl_->EvictedUIResourcesExist())
1129 layer_tree_host_impl_->SetNeedsCommit();
[email protected]c9280762013-08-01 06:28:571130}
1131
danakj7383c552015-02-11 18:01:481132void LayerTreeImpl::RegisterPictureLayerImpl(PictureLayerImpl* layer) {
1133 DCHECK(std::find(picture_layers_.begin(), picture_layers_.end(), layer) ==
1134 picture_layers_.end());
1135 picture_layers_.push_back(layer);
1136}
1137
1138void LayerTreeImpl::UnregisterPictureLayerImpl(PictureLayerImpl* layer) {
1139 std::vector<PictureLayerImpl*>::iterator it =
1140 std::find(picture_layers_.begin(), picture_layers_.end(), layer);
1141 DCHECK(it != picture_layers_.end());
1142 picture_layers_.erase(it);
1143}
1144
[email protected]30fe19ff2013-07-04 00:54:451145void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl* layer) {
1146 // Only the active tree needs to know about layers with copy requests, as
1147 // they are aborted if not serviced during draw.
1148 DCHECK(IsActiveTree());
1149
[email protected]a4ee12812014-02-06 17:33:381150 // DCHECK(std::find(layers_with_copy_output_request_.begin(),
1151 // layers_with_copy_output_request_.end(),
1152 // layer) == layers_with_copy_output_request_.end());
1153 // TODO(danakj): Remove this once crash is found crbug.com/309777
1154 for (size_t i = 0; i < layers_with_copy_output_request_.size(); ++i) {
1155 CHECK(layers_with_copy_output_request_[i] != layer)
1156 << i << " of " << layers_with_copy_output_request_.size();
1157 }
[email protected]30fe19ff2013-07-04 00:54:451158 layers_with_copy_output_request_.push_back(layer);
1159}
1160
1161void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl* layer) {
1162 // Only the active tree needs to know about layers with copy requests, as
1163 // they are aborted if not serviced during draw.
1164 DCHECK(IsActiveTree());
1165
1166 std::vector<LayerImpl*>::iterator it = std::find(
1167 layers_with_copy_output_request_.begin(),
1168 layers_with_copy_output_request_.end(),
1169 layer);
1170 DCHECK(it != layers_with_copy_output_request_.end());
[email protected]f5de9e5b2013-07-30 22:26:571171 layers_with_copy_output_request_.erase(it);
[email protected]a4ee12812014-02-06 17:33:381172
1173 // TODO(danakj): Remove this once crash is found crbug.com/309777
1174 for (size_t i = 0; i < layers_with_copy_output_request_.size(); ++i) {
1175 CHECK(layers_with_copy_output_request_[i] != layer)
1176 << i << " of " << layers_with_copy_output_request_.size();
1177 }
[email protected]30fe19ff2013-07-04 00:54:451178}
1179
[email protected]53526372013-12-07 04:31:501180const std::vector<LayerImpl*>& LayerTreeImpl::LayersWithCopyOutputRequest()
[email protected]30fe19ff2013-07-04 00:54:451181 const {
1182 // Only the active tree needs to know about layers with copy requests, as
1183 // they are aborted if not serviced during draw.
1184 DCHECK(IsActiveTree());
1185
1186 return layers_with_copy_output_request_;
1187}
1188
[email protected]28336d52014-05-12 19:07:281189template <typename LayerType>
1190static inline bool LayerClipsSubtree(LayerType* layer) {
1191 return layer->masks_to_bounds() || layer->mask_layer();
1192}
1193
1194static bool PointHitsRect(
1195 const gfx::PointF& screen_space_point,
1196 const gfx::Transform& local_space_to_screen_space_transform,
1197 const gfx::RectF& local_space_rect,
1198 float* distance_to_camera) {
1199 // If the transform is not invertible, then assume that this point doesn't hit
1200 // this rect.
1201 gfx::Transform inverse_local_space_to_screen_space(
1202 gfx::Transform::kSkipInitialization);
1203 if (!local_space_to_screen_space_transform.GetInverse(
1204 &inverse_local_space_to_screen_space))
1205 return false;
1206
1207 // Transform the hit test point from screen space to the local space of the
1208 // given rect.
1209 bool clipped = false;
1210 gfx::Point3F planar_point = MathUtil::ProjectPoint3D(
1211 inverse_local_space_to_screen_space, screen_space_point, &clipped);
1212 gfx::PointF hit_test_point_in_local_space =
1213 gfx::PointF(planar_point.x(), planar_point.y());
1214
1215 // If ProjectPoint could not project to a valid value, then we assume that
1216 // this point doesn't hit this rect.
1217 if (clipped)
1218 return false;
1219
1220 if (!local_space_rect.Contains(hit_test_point_in_local_space))
1221 return false;
1222
1223 if (distance_to_camera) {
1224 // To compute the distance to the camera, we have to take the planar point
1225 // and pull it back to world space and compute the displacement along the
1226 // z-axis.
1227 gfx::Point3F planar_point_in_screen_space(planar_point);
1228 local_space_to_screen_space_transform.TransformPoint(
1229 &planar_point_in_screen_space);
1230 *distance_to_camera = planar_point_in_screen_space.z();
1231 }
1232
1233 return true;
1234}
1235
1236static bool PointHitsRegion(const gfx::PointF& screen_space_point,
1237 const gfx::Transform& screen_space_transform,
1238 const Region& layer_space_region,
1239 float layer_content_scale_x,
1240 float layer_content_scale_y) {
1241 // If the transform is not invertible, then assume that this point doesn't hit
1242 // this region.
1243 gfx::Transform inverse_screen_space_transform(
1244 gfx::Transform::kSkipInitialization);
1245 if (!screen_space_transform.GetInverse(&inverse_screen_space_transform))
1246 return false;
1247
1248 // Transform the hit test point from screen space to the local space of the
1249 // given region.
1250 bool clipped = false;
1251 gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint(
1252 inverse_screen_space_transform, screen_space_point, &clipped);
1253 gfx::PointF hit_test_point_in_layer_space =
1254 gfx::ScalePoint(hit_test_point_in_content_space,
1255 1.f / layer_content_scale_x,
1256 1.f / layer_content_scale_y);
1257
1258 // If ProjectPoint could not project to a valid value, then we assume that
1259 // this point doesn't hit this region.
1260 if (clipped)
1261 return false;
1262
1263 return layer_space_region.Contains(
1264 gfx::ToRoundedPoint(hit_test_point_in_layer_space));
1265}
1266
[email protected]19aec372014-07-01 19:08:491267static const LayerImpl* GetNextClippingLayer(const LayerImpl* layer) {
[email protected]0ec86f52014-06-12 20:54:031268 if (layer->scroll_parent())
1269 return layer->scroll_parent();
1270 if (layer->clip_parent())
1271 return layer->clip_parent();
1272 return layer->parent();
1273}
1274
[email protected]28336d52014-05-12 19:07:281275static bool PointIsClippedBySurfaceOrClipRect(
1276 const gfx::PointF& screen_space_point,
[email protected]19aec372014-07-01 19:08:491277 const LayerImpl* layer) {
[email protected]28336d52014-05-12 19:07:281278 // Walk up the layer tree and hit-test any render_surfaces and any layer
1279 // clip rects that are active.
[email protected]0ec86f52014-06-12 20:54:031280 for (; layer; layer = GetNextClippingLayer(layer)) {
1281 if (layer->render_surface() &&
1282 !PointHitsRect(screen_space_point,
1283 layer->render_surface()->screen_space_transform(),
1284 layer->render_surface()->content_rect(),
1285 NULL))
[email protected]28336d52014-05-12 19:07:281286 return true;
1287
[email protected]0ec86f52014-06-12 20:54:031288 if (LayerClipsSubtree(layer) &&
1289 !PointHitsRect(screen_space_point,
1290 layer->screen_space_transform(),
1291 gfx::Rect(layer->content_bounds()),
1292 NULL))
[email protected]28336d52014-05-12 19:07:281293 return true;
[email protected]28336d52014-05-12 19:07:281294 }
1295
1296 // If we have finished walking all ancestors without having already exited,
1297 // then the point is not clipped by any ancestors.
1298 return false;
1299}
1300
[email protected]19aec372014-07-01 19:08:491301static bool PointHitsLayer(const LayerImpl* layer,
[email protected]28336d52014-05-12 19:07:281302 const gfx::PointF& screen_space_point,
1303 float* distance_to_intersection) {
1304 gfx::RectF content_rect(layer->content_bounds());
1305 if (!PointHitsRect(screen_space_point,
1306 layer->screen_space_transform(),
1307 content_rect,
1308 distance_to_intersection))
1309 return false;
1310
1311 // At this point, we think the point does hit the layer, but we need to walk
1312 // up the parents to ensure that the layer was not clipped in such a way
1313 // that the hit point actually should not hit the layer.
1314 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer))
1315 return false;
1316
1317 // Skip the HUD layer.
1318 if (layer == layer->layer_tree_impl()->hud_layer())
1319 return false;
1320
1321 return true;
1322}
1323
1324struct FindClosestMatchingLayerDataForRecursion {
1325 FindClosestMatchingLayerDataForRecursion()
1326 : closest_match(NULL),
1327 closest_distance(-std::numeric_limits<float>::infinity()) {}
1328 LayerImpl* closest_match;
1329 // Note that the positive z-axis points towards the camera, so bigger means
1330 // closer in this case, counterintuitively.
1331 float closest_distance;
1332};
1333
1334template <typename Functor>
1335static void FindClosestMatchingLayer(
1336 const gfx::PointF& screen_space_point,
1337 LayerImpl* layer,
1338 const Functor& func,
1339 FindClosestMatchingLayerDataForRecursion* data_for_recursion) {
1340 for (int i = layer->children().size() - 1; i >= 0; --i) {
1341 FindClosestMatchingLayer(
1342 screen_space_point, layer->children()[i], func, data_for_recursion);
1343 }
1344
1345 float distance_to_intersection = 0.f;
1346 if (func(layer) &&
1347 PointHitsLayer(layer, screen_space_point, &distance_to_intersection) &&
1348 ((!data_for_recursion->closest_match ||
1349 distance_to_intersection > data_for_recursion->closest_distance))) {
1350 data_for_recursion->closest_distance = distance_to_intersection;
1351 data_for_recursion->closest_match = layer;
1352 }
1353}
1354
1355static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl* layer) {
1356 if (!layer->scrollable())
1357 return false;
majidvp6cfcc362015-03-06 20:46:391358 if (layer->draw_properties().layer_or_descendant_is_drawn)
[email protected]28336d52014-05-12 19:07:281359 return true;
majidvp6cfcc362015-03-06 20:46:391360
[email protected]28336d52014-05-12 19:07:281361 if (!layer->scroll_children())
1362 return false;
1363 for (std::set<LayerImpl*>::const_iterator it =
1364 layer->scroll_children()->begin();
1365 it != layer->scroll_children()->end();
1366 ++it) {
majidvp6cfcc362015-03-06 20:46:391367 if ((*it)->draw_properties().layer_or_descendant_is_drawn)
[email protected]28336d52014-05-12 19:07:281368 return true;
1369 }
1370 return false;
1371}
1372
1373struct FindScrollingLayerFunctor {
1374 bool operator()(LayerImpl* layer) const {
1375 return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer);
1376 }
1377};
1378
1379LayerImpl* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint(
1380 const gfx::PointF& screen_space_point) {
1381 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1382 FindClosestMatchingLayer(screen_space_point,
1383 root_layer(),
1384 FindScrollingLayerFunctor(),
1385 &data_for_recursion);
1386 return data_for_recursion.closest_match;
1387}
1388
1389struct HitTestVisibleScrollableOrTouchableFunctor {
1390 bool operator()(LayerImpl* layer) const {
1391 return layer->IsDrawnRenderSurfaceLayerListMember() ||
1392 ScrollsAnyDrawnRenderSurfaceLayerListMember(layer) ||
1393 !layer->touch_event_handler_region().IsEmpty() ||
1394 layer->have_wheel_event_handlers();
1395 }
1396};
1397
1398LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPoint(
1399 const gfx::PointF& screen_space_point) {
[email protected]8f7f298822014-06-13 00:23:321400 if (!root_layer())
1401 return NULL;
enneaf5bda32015-02-19 01:27:361402 bool update_lcd_text = false;
1403 if (!UpdateDrawProperties(update_lcd_text))
[email protected]8f7f298822014-06-13 00:23:321404 return NULL;
[email protected]28336d52014-05-12 19:07:281405 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1406 FindClosestMatchingLayer(screen_space_point,
1407 root_layer(),
1408 HitTestVisibleScrollableOrTouchableFunctor(),
1409 &data_for_recursion);
1410 return data_for_recursion.closest_match;
1411}
1412
1413static bool LayerHasTouchEventHandlersAt(const gfx::PointF& screen_space_point,
1414 LayerImpl* layer_impl) {
1415 if (layer_impl->touch_event_handler_region().IsEmpty())
1416 return false;
1417
1418 if (!PointHitsRegion(screen_space_point,
1419 layer_impl->screen_space_transform(),
1420 layer_impl->touch_event_handler_region(),
1421 layer_impl->contents_scale_x(),
1422 layer_impl->contents_scale_y()))
1423 return false;
1424
1425 // At this point, we think the point does hit the touch event handler region
1426 // on the layer, but we need to walk up the parents to ensure that the layer
1427 // was not clipped in such a way that the hit point actually should not hit
1428 // the layer.
1429 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl))
1430 return false;
1431
1432 return true;
1433}
1434
ccameron3b607362015-02-02 22:46:291435struct FindWheelEventLayerFunctor {
1436 bool operator()(LayerImpl* layer) const {
1437 return layer->have_wheel_event_handlers();
1438 }
1439};
1440
1441LayerImpl* LayerTreeImpl::FindLayerWithWheelHandlerThatIsHitByPoint(
1442 const gfx::PointF& screen_space_point) {
1443 if (!root_layer())
1444 return NULL;
enneaf5bda32015-02-19 01:27:361445 bool update_lcd_text = false;
1446 if (!UpdateDrawProperties(update_lcd_text))
ccameron3b607362015-02-02 22:46:291447 return NULL;
1448 FindWheelEventLayerFunctor func;
1449 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1450 FindClosestMatchingLayer(screen_space_point, root_layer(), func,
1451 &data_for_recursion);
1452 return data_for_recursion.closest_match;
1453}
1454
[email protected]28336d52014-05-12 19:07:281455struct FindTouchEventLayerFunctor {
1456 bool operator()(LayerImpl* layer) const {
1457 return LayerHasTouchEventHandlersAt(screen_space_point, layer);
1458 }
1459 const gfx::PointF screen_space_point;
1460};
1461
1462LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
1463 const gfx::PointF& screen_space_point) {
[email protected]8f7f298822014-06-13 00:23:321464 if (!root_layer())
1465 return NULL;
enneaf5bda32015-02-19 01:27:361466 bool update_lcd_text = false;
1467 if (!UpdateDrawProperties(update_lcd_text))
[email protected]8f7f298822014-06-13 00:23:321468 return NULL;
[email protected]28336d52014-05-12 19:07:281469 FindTouchEventLayerFunctor func = {screen_space_point};
1470 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1471 FindClosestMatchingLayer(
1472 screen_space_point, root_layer(), func, &data_for_recursion);
1473 return data_for_recursion.closest_match;
1474}
1475
jdduke449b5292015-04-23 19:36:441476void LayerTreeImpl::RegisterSelection(const LayerSelection& selection) {
1477 selection_ = selection;
[email protected]19aec372014-07-01 19:08:491478}
1479
jdduke449b5292015-04-23 19:36:441480static ViewportSelectionBound ComputeViewportSelectionBound(
[email protected]6ef948732014-08-22 18:57:441481 const LayerSelectionBound& layer_bound,
[email protected]19aec372014-07-01 19:08:491482 LayerImpl* layer,
1483 float device_scale_factor) {
[email protected]6ef948732014-08-22 18:57:441484 ViewportSelectionBound viewport_bound;
1485 viewport_bound.type = layer_bound.type;
[email protected]19aec372014-07-01 19:08:491486
[email protected]6ef948732014-08-22 18:57:441487 if (!layer || layer_bound.type == SELECTION_BOUND_EMPTY)
1488 return viewport_bound;
[email protected]19aec372014-07-01 19:08:491489
[email protected]6ef948732014-08-22 18:57:441490 gfx::PointF layer_scaled_top = gfx::ScalePoint(layer_bound.edge_top,
1491 layer->contents_scale_x(),
1492 layer->contents_scale_y());
1493 gfx::PointF layer_scaled_bottom = gfx::ScalePoint(layer_bound.edge_bottom,
1494 layer->contents_scale_x(),
1495 layer->contents_scale_y());
[email protected]19aec372014-07-01 19:08:491496
[email protected]6ef948732014-08-22 18:57:441497 bool clipped = false;
1498 gfx::PointF screen_top = MathUtil::MapPoint(
1499 layer->screen_space_transform(), layer_scaled_top, &clipped);
1500 gfx::PointF screen_bottom = MathUtil::MapPoint(
1501 layer->screen_space_transform(), layer_scaled_bottom, &clipped);
1502
1503 const float inv_scale = 1.f / device_scale_factor;
1504 viewport_bound.edge_top = gfx::ScalePoint(screen_top, inv_scale);
1505 viewport_bound.edge_bottom = gfx::ScalePoint(screen_bottom, inv_scale);
1506
1507 // The bottom edge point is used for visibility testing as it is the logical
[email protected]19aec372014-07-01 19:08:491508 // focal point for bound selection handles (this may change in the future).
[email protected]6ef948732014-08-22 18:57:441509 // Shifting the visibility point fractionally inward ensures that neighboring
1510 // or logically coincident layers aligned to integral DPI coordinates will not
1511 // spuriously occlude the bound.
1512 gfx::Vector2dF visibility_offset = layer_scaled_top - layer_scaled_bottom;
1513 visibility_offset.Scale(device_scale_factor / visibility_offset.Length());
1514 gfx::PointF visibility_point = layer_scaled_bottom + visibility_offset;
1515 if (visibility_point.x() <= 0)
1516 visibility_point.set_x(visibility_point.x() + device_scale_factor);
1517 visibility_point = MathUtil::MapPoint(
1518 layer->screen_space_transform(), visibility_point, &clipped);
1519
[email protected]19aec372014-07-01 19:08:491520 float intersect_distance = 0.f;
[email protected]6ef948732014-08-22 18:57:441521 viewport_bound.visible =
1522 PointHitsLayer(layer, visibility_point, &intersect_distance);
[email protected]19aec372014-07-01 19:08:491523
[email protected]6ef948732014-08-22 18:57:441524 return viewport_bound;
[email protected]19aec372014-07-01 19:08:491525}
1526
jdduke449b5292015-04-23 19:36:441527void LayerTreeImpl::GetViewportSelection(ViewportSelection* selection) {
1528 DCHECK(selection);
[email protected]19aec372014-07-01 19:08:491529
jdduke449b5292015-04-23 19:36:441530 selection->start = ComputeViewportSelectionBound(
1531 selection_.start,
1532 selection_.start.layer_id ? LayerById(selection_.start.layer_id) : NULL,
[email protected]19aec372014-07-01 19:08:491533 device_scale_factor());
jdduke449b5292015-04-23 19:36:441534 selection->is_editable = selection_.is_editable;
1535 selection->is_empty_text_form_control = selection_.is_empty_text_form_control;
1536 if (selection->start.type == SELECTION_BOUND_CENTER ||
1537 selection->start.type == SELECTION_BOUND_EMPTY) {
1538 selection->end = selection->start;
[email protected]19aec372014-07-01 19:08:491539 } else {
jdduke449b5292015-04-23 19:36:441540 selection->end = ComputeViewportSelectionBound(
1541 selection_.end,
1542 selection_.end.layer_id ? LayerById(selection_.end.layer_id) : NULL,
[email protected]19aec372014-07-01 19:08:491543 device_scale_factor());
1544 }
1545}
1546
[email protected]749cbc62014-07-10 01:06:351547void LayerTreeImpl::InputScrollAnimationFinished() {
1548 layer_tree_host_impl_->ScrollEnd();
1549}
1550
vmpstr56ace232014-10-09 20:16:281551bool LayerTreeImpl::SmoothnessTakesPriority() const {
1552 return layer_tree_host_impl_->GetTreePriority() == SMOOTHNESS_TAKES_PRIORITY;
1553}
1554
skyostil3976a3f2014-09-04 22:07:231555BlockingTaskRunner* LayerTreeImpl::BlockingMainThreadTaskRunner() const {
1556 return proxy()->blocking_main_thread_task_runner();
1557}
1558
sunnyps7d073dc2015-04-16 23:29:121559VideoFrameControllerClient* LayerTreeImpl::GetVideoFrameControllerClient()
1560 const {
1561 return layer_tree_host_impl_;
1562}
1563
bokanfcdbc182014-11-21 21:53:331564void LayerTreeImpl::SetPendingPageScaleAnimation(
1565 scoped_ptr<PendingPageScaleAnimation> pending_animation) {
1566 pending_page_scale_animation_ = pending_animation.Pass();
bokan915bf352014-10-02 21:57:141567}
1568
bokanfcdbc182014-11-21 21:53:331569scoped_ptr<PendingPageScaleAnimation>
1570 LayerTreeImpl::TakePendingPageScaleAnimation() {
1571 return pending_page_scale_animation_.Pass();
bokan915bf352014-10-02 21:57:141572}
1573
[email protected]ca2902e92013-03-28 01:45:351574} // namespace cc