blob: 5fc01fc9d2dbeeef00999624053ac90a670c7f34 [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
[email protected]28336d52014-05-12 19:07:287#include <limits>
8#include <set>
9
[email protected]76ffd9e2012-12-20 19:12:4710#include "base/debug/trace_event.h"
[email protected]d12aa932014-08-01 13:10:3811#include "base/debug/trace_event_argument.h"
[email protected]95e4e1a02013-03-18 07:09:0912#include "cc/animation/keyframed_animation_curve.h"
13#include "cc/animation/scrollbar_animation_controller.h"
[email protected]930ff43b2014-05-02 05:24:0014#include "cc/animation/scrollbar_animation_controller_linear_fade.h"
15#include "cc/animation/scrollbar_animation_controller_thinning.h"
[email protected]3744e27b2013-11-06 21:44:0816#include "cc/base/math_util.h"
17#include "cc/base/util.h"
[email protected]12a63da2014-06-13 06:06:2218#include "cc/debug/devtools_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2219#include "cc/debug/traced_value.h"
bokan915bf352014-10-02 21:57:1420#include "cc/input/page_scale_animation.h"
[email protected]cc3cfaa2013-03-18 09:05:5221#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]57ac9482013-09-17 21:13:3922#include "cc/layers/layer.h"
[email protected]34ba1ffb2014-03-05 06:55:0323#include "cc/layers/layer_iterator.h"
[email protected]50761e92013-03-29 20:51:2824#include "cc/layers/render_surface_impl.h"
[email protected]80413d72013-08-30 20:25:3325#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]e1042192013-11-08 05:44:2426#include "cc/resources/ui_resource_request.h"
[email protected]556fd292013-03-18 08:03:0427#include "cc/trees/layer_tree_host_common.h"
28#include "cc/trees/layer_tree_host_impl.h"
[email protected]562b7ad2014-06-23 22:17:1129#include "cc/trees/occlusion_tracker.h"
[email protected]28336d52014-05-12 19:07:2830#include "ui/gfx/point_conversions.h"
[email protected]ffb2720f2013-03-15 19:18:3731#include "ui/gfx/size_conversions.h"
[email protected]caa567d2012-12-20 07:56:1632#include "ui/gfx/vector2d_conversions.h"
[email protected]3b31c6ac2012-12-06 21:27:2933
34namespace cc {
35
[email protected]adeda572014-01-31 00:49:4736// This class exists to split the LayerScrollOffsetDelegate between the
37// InnerViewportScrollLayer and the OuterViewportScrollLayer in a manner
38// that never requires the embedder or LayerImpl to know about.
[email protected]ec2322e2014-05-15 16:32:0039class LayerScrollOffsetDelegateProxy : public LayerImpl::ScrollOffsetDelegate {
[email protected]adeda572014-01-31 00:49:4740 public:
41 LayerScrollOffsetDelegateProxy(LayerImpl* layer,
42 LayerScrollOffsetDelegate* delegate,
43 LayerTreeImpl* layer_tree)
44 : layer_(layer), delegate_(delegate), layer_tree_impl_(layer_tree) {}
[email protected]ec2322e2014-05-15 16:32:0045 virtual ~LayerScrollOffsetDelegateProxy() {}
[email protected]adeda572014-01-31 00:49:4746
miletusf57925d2014-10-01 19:38:1347 gfx::ScrollOffset last_set_scroll_offset() const {
[email protected]adeda572014-01-31 00:49:4748 return last_set_scroll_offset_;
49 }
50
51 // LayerScrollOffsetDelegate implementation.
miletusf57925d2014-10-01 19:38:1352 virtual void SetTotalScrollOffset(
mostynbf68776d82014-10-06 18:07:3753 const gfx::ScrollOffset& new_offset) override {
[email protected]adeda572014-01-31 00:49:4754 last_set_scroll_offset_ = new_offset;
55 layer_tree_impl_->UpdateScrollOffsetDelegate();
56 }
57
mostynbf68776d82014-10-06 18:07:3758 virtual gfx::ScrollOffset GetTotalScrollOffset() override {
[email protected]adeda572014-01-31 00:49:4759 return layer_tree_impl_->GetDelegatedScrollOffset(layer_);
60 }
61
mostynbf68776d82014-10-06 18:07:3762 virtual bool IsExternalFlingActive() const override {
[email protected]adeda572014-01-31 00:49:4763 return delegate_->IsExternalFlingActive();
64 }
65
[email protected]adeda572014-01-31 00:49:4766 private:
67 LayerImpl* layer_;
68 LayerScrollOffsetDelegate* delegate_;
69 LayerTreeImpl* layer_tree_impl_;
miletusf57925d2014-10-01 19:38:1370 gfx::ScrollOffset last_set_scroll_offset_;
[email protected]adeda572014-01-31 00:49:4771};
72
[email protected]8bef40572012-12-11 21:38:0873LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl)
[email protected]db8259f2013-02-01 05:25:0474 : layer_tree_host_impl_(layer_tree_host_impl),
75 source_frame_number_(-1),
76 hud_layer_(0),
[email protected]1960a712013-04-30 17:06:4777 currently_scrolling_layer_(NULL),
78 root_layer_scroll_offset_delegate_(NULL),
[email protected]db8259f2013-02-01 05:25:0479 background_color_(0),
80 has_transparent_background_(false),
[email protected]57ac9482013-09-17 21:13:3981 page_scale_layer_(NULL),
82 inner_viewport_scroll_layer_(NULL),
83 outer_viewport_scroll_layer_(NULL),
[email protected]db8259f2013-02-01 05:25:0484 page_scale_factor_(1),
85 page_scale_delta_(1),
86 sent_page_scale_delta_(1),
87 min_page_scale_factor_(0),
88 max_page_scale_factor_(0),
89 scrolling_layer_id_from_previous_tree_(0),
90 contents_textures_purged_(false),
[email protected]318822852013-02-14 00:54:2791 viewport_size_invalid_(false),
[email protected]db8259f2013-02-01 05:25:0492 needs_update_draw_properties_(true),
[email protected]7d08a9352013-10-15 08:24:5693 needs_full_tree_sync_(true),
[email protected]390bb1ff2014-05-09 17:14:4094 next_activation_forces_redraw_(false),
[email protected]759dc9f2014-07-23 19:18:5195 has_ever_been_drawn_(false),
bokan88eae012014-09-09 20:40:4296 render_surface_layer_list_id_(0),
97 top_controls_layout_height_(0),
bokan55b2f152014-09-15 14:47:5998 top_controls_content_offset_(0),
bokan88eae012014-09-09 20:40:4299 top_controls_delta_(0),
100 sent_top_controls_delta_(0) {
[email protected]390bb1ff2014-05-09 17:14:40101}
[email protected]3b31c6ac2012-12-06 21:27:29102
103LayerTreeImpl::~LayerTreeImpl() {
[email protected]586871b2014-07-22 17:05:11104 BreakSwapPromises(SwapPromise::SWAP_FAILS);
105
[email protected]361bc00d2012-12-14 07:03:24106 // Need to explicitly clear the tree prior to destroying this so that
107 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
[email protected]df17af52014-02-06 02:20:40108 DCHECK(!root_layer_);
109 DCHECK(layers_with_copy_output_request_.empty());
[email protected]3b31c6ac2012-12-06 21:27:29110}
111
danakjf446a072014-09-27 21:55:48112void LayerTreeImpl::Shutdown() {
113 root_layer_ = nullptr;
114}
[email protected]df17af52014-02-06 02:20:40115
[email protected]aeef2f02014-05-10 12:15:48116void LayerTreeImpl::ReleaseResources() {
117 if (root_layer_)
118 ReleaseResourcesRecursive(root_layer_.get());
119}
120
[email protected]d35cd7b22014-01-29 14:32:46121void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
[email protected]adeda572014-01-31 00:49:47122 if (inner_viewport_scroll_layer_)
123 inner_viewport_scroll_layer_->SetScrollOffsetDelegate(NULL);
124 if (outer_viewport_scroll_layer_)
125 outer_viewport_scroll_layer_->SetScrollOffsetDelegate(NULL);
danakjf446a072014-09-27 21:55:48126 inner_viewport_scroll_delegate_proxy_ = nullptr;
127 outer_viewport_scroll_delegate_proxy_ = nullptr;
[email protected]adeda572014-01-31 00:49:47128
[email protected]3b31c6ac2012-12-06 21:27:29129 root_layer_ = layer.Pass();
[email protected]5c4824e12013-01-12 16:34:53130 currently_scrolling_layer_ = NULL;
[email protected]adeda572014-01-31 00:49:47131 inner_viewport_scroll_layer_ = NULL;
132 outer_viewport_scroll_layer_ = NULL;
133 page_scale_layer_ = NULL;
[email protected]5c4824e12013-01-12 16:34:53134
[email protected]c1bb5af2013-03-13 19:06:27135 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]5c4824e12013-01-12 16:34:53136}
137
[email protected]adeda572014-01-31 00:49:47138LayerImpl* LayerTreeImpl::InnerViewportScrollLayer() const {
139 return inner_viewport_scroll_layer_;
140}
[email protected]3b31c6ac2012-12-06 21:27:29141
[email protected]adeda572014-01-31 00:49:47142LayerImpl* LayerTreeImpl::OuterViewportScrollLayer() const {
143 return outer_viewport_scroll_layer_;
144}
[email protected]1960a712013-04-30 17:06:47145
miletusf57925d2014-10-01 19:38:13146gfx::ScrollOffset LayerTreeImpl::TotalScrollOffset() const {
147 gfx::ScrollOffset offset;
[email protected]3b31c6ac2012-12-06 21:27:29148
[email protected]adeda572014-01-31 00:49:47149 if (inner_viewport_scroll_layer_)
150 offset += inner_viewport_scroll_layer_->TotalScrollOffset();
151
152 if (outer_viewport_scroll_layer_)
153 offset += outer_viewport_scroll_layer_->TotalScrollOffset();
154
155 return offset;
156}
157
miletusf57925d2014-10-01 19:38:13158gfx::ScrollOffset LayerTreeImpl::TotalMaxScrollOffset() const {
159 gfx::ScrollOffset offset;
[email protected]adeda572014-01-31 00:49:47160
161 if (inner_viewport_scroll_layer_)
162 offset += inner_viewport_scroll_layer_->MaxScrollOffset();
163
164 if (outer_viewport_scroll_layer_)
165 offset += outer_viewport_scroll_layer_->MaxScrollOffset();
166
167 return offset;
168}
169gfx::Vector2dF LayerTreeImpl::TotalScrollDelta() const {
170 DCHECK(inner_viewport_scroll_layer_);
171 gfx::Vector2dF delta = inner_viewport_scroll_layer_->ScrollDelta();
172
173 if (outer_viewport_scroll_layer_)
174 delta += outer_viewport_scroll_layer_->ScrollDelta();
175
176 return delta;
[email protected]3b31c6ac2012-12-06 21:27:29177}
178
179scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
180 // Clear all data structures that have direct references to the layer tree.
181 scrolling_layer_id_from_previous_tree_ =
182 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
[email protected]adeda572014-01-31 00:49:47183 if (inner_viewport_scroll_layer_)
184 inner_viewport_scroll_layer_->SetScrollOffsetDelegate(NULL);
185 if (outer_viewport_scroll_layer_)
186 outer_viewport_scroll_layer_->SetScrollOffsetDelegate(NULL);
danakjf446a072014-09-27 21:55:48187 inner_viewport_scroll_delegate_proxy_ = nullptr;
188 outer_viewport_scroll_delegate_proxy_ = nullptr;
[email protected]adeda572014-01-31 00:49:47189 inner_viewport_scroll_layer_ = NULL;
190 outer_viewport_scroll_layer_ = NULL;
191 page_scale_layer_ = NULL;
[email protected]3b31c6ac2012-12-06 21:27:29192 currently_scrolling_layer_ = NULL;
193
[email protected]76ffd9e2012-12-20 19:12:47194 render_surface_layer_list_.clear();
[email protected]615c78a2013-01-24 23:44:16195 set_needs_update_draw_properties();
[email protected]3b31c6ac2012-12-06 21:27:29196 return root_layer_.Pass();
197}
198
[email protected]7aba6662013-03-12 10:17:34199void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl* target_tree) {
[email protected]c9280762013-08-01 06:28:57200 // The request queue should have been processed and does not require a push.
201 DCHECK_EQ(ui_resource_request_queue_.size(), 0u);
202
[email protected]7d08a9352013-10-15 08:24:56203 if (next_activation_forces_redraw_) {
[email protected]12a63da2014-06-13 06:06:22204 target_tree->ForceRedrawNextActivation();
[email protected]7d08a9352013-10-15 08:24:56205 next_activation_forces_redraw_ = false;
206 }
207
[email protected]b69c1db2013-11-27 00:05:19208 target_tree->PassSwapPromises(&swap_promise_list_);
209
bokan88eae012014-09-09 20:40:42210 target_tree->top_controls_layout_height_ = top_controls_layout_height_;
bokan55b2f152014-09-15 14:47:59211 target_tree->top_controls_content_offset_ = top_controls_content_offset_;
bokan88eae012014-09-09 20:40:42212 target_tree->top_controls_delta_ =
213 target_tree->top_controls_delta_ -
214 target_tree->sent_top_controls_delta_;
215 target_tree->sent_top_controls_delta_ = 0.f;
216
[email protected]d6021f6a2014-06-12 21:15:24217 target_tree->SetPageScaleValues(
218 page_scale_factor(), min_page_scale_factor(), max_page_scale_factor(),
[email protected]c60279472013-01-30 12:10:51219 target_tree->page_scale_delta() / target_tree->sent_page_scale_delta());
220 target_tree->set_sent_page_scale_delta(1);
221
bokan915bf352014-10-02 21:57:14222 target_tree->page_scale_animation_ = page_scale_animation_.Pass();
223
[email protected]adeda572014-01-31 00:49:47224 if (page_scale_layer_ && inner_viewport_scroll_layer_) {
[email protected]57ac9482013-09-17 21:13:39225 target_tree->SetViewportLayersFromIds(
226 page_scale_layer_->id(),
227 inner_viewport_scroll_layer_->id(),
228 outer_viewport_scroll_layer_ ? outer_viewport_scroll_layer_->id()
229 : Layer::INVALID_ID);
[email protected]adeda572014-01-31 00:49:47230 } else {
231 target_tree->ClearViewportLayers();
[email protected]57ac9482013-09-17 21:13:39232 }
[email protected]19aec372014-07-01 19:08:49233
[email protected]ebb179b2014-07-16 17:54:41234 target_tree->RegisterSelection(selection_start_, selection_end_);
[email protected]19aec372014-07-01 19:08:49235
[email protected]c60279472013-01-30 12:10:51236 // This should match the property synchronization in
237 // LayerTreeHost::finishCommitOnImplThread().
238 target_tree->set_source_frame_number(source_frame_number());
239 target_tree->set_background_color(background_color());
240 target_tree->set_has_transparent_background(has_transparent_background());
241
242 if (ContentsTexturesPurged())
243 target_tree->SetContentsTexturesPurged();
244 else
245 target_tree->ResetContentsTexturesPurged();
246
[email protected]318822852013-02-14 00:54:27247 if (ViewportSizeInvalid())
248 target_tree->SetViewportSizeInvalid();
249 else
250 target_tree->ResetViewportSizeInvalid();
251
[email protected]c60279472013-01-30 12:10:51252 if (hud_layer())
253 target_tree->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl*>(
[email protected]6ba914122013-03-22 16:26:39254 LayerTreeHostCommon::FindLayerInSubtree(
[email protected]c1bb5af2013-03-13 19:06:27255 target_tree->root_layer(), hud_layer()->id())));
[email protected]c60279472013-01-30 12:10:51256 else
257 target_tree->set_hud_layer(NULL);
[email protected]759dc9f2014-07-23 19:18:51258
259 target_tree->has_ever_been_drawn_ = false;
[email protected]c60279472013-01-30 12:10:51260}
261
[email protected]fef74fd2014-02-27 06:28:17262LayerImpl* LayerTreeImpl::InnerViewportContainerLayer() const {
263 return inner_viewport_scroll_layer_
264 ? inner_viewport_scroll_layer_->scroll_clip_layer()
265 : NULL;
[email protected]ffb2720f2013-03-15 19:18:37266}
267
bokanef971462014-10-13 22:58:32268LayerImpl* LayerTreeImpl::OuterViewportContainerLayer() const {
269 return outer_viewport_scroll_layer_
270 ? outer_viewport_scroll_layer_->scroll_clip_layer()
271 : NULL;
272}
273
[email protected]ffb2720f2013-03-15 19:18:37274LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() const {
[email protected]69b50ec2013-01-19 04:58:01275 DCHECK(IsActiveTree());
276 return currently_scrolling_layer_;
277}
278
[email protected]0fc818e2013-03-18 06:45:20279void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl* layer) {
280 if (currently_scrolling_layer_ == layer)
281 return;
282
283 if (currently_scrolling_layer_ &&
284 currently_scrolling_layer_->scrollbar_animation_controller())
[email protected]1dc06162014-03-26 22:54:45285 currently_scrolling_layer_->scrollbar_animation_controller()
[email protected]930ff43b2014-05-02 05:24:00286 ->DidScrollEnd();
[email protected]0fc818e2013-03-18 06:45:20287 currently_scrolling_layer_ = layer;
288 if (layer && layer->scrollbar_animation_controller())
[email protected]930ff43b2014-05-02 05:24:00289 layer->scrollbar_animation_controller()->DidScrollBegin();
[email protected]0fc818e2013-03-18 06:45:20290}
291
[email protected]3b31c6ac2012-12-06 21:27:29292void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
[email protected]0fc818e2013-03-18 06:45:20293 SetCurrentlyScrollingLayer(NULL);
[email protected]3b31c6ac2012-12-06 21:27:29294 scrolling_layer_id_from_previous_tree_ = 0;
295}
296
[email protected]adeda572014-01-31 00:49:47297namespace {
298
299void ForceScrollbarParameterUpdateAfterScaleChange(LayerImpl* current_layer) {
300 if (!current_layer)
301 return;
302
303 while (current_layer) {
sataya.m07f11a82014-10-07 14:29:18304 current_layer->ScrollbarParametersDidChange(false);
[email protected]adeda572014-01-31 00:49:47305 current_layer = current_layer->parent();
306 }
307}
308
309} // namespace
310
[email protected]c60279472013-01-30 12:10:51311void LayerTreeImpl::SetPageScaleFactorAndLimits(float page_scale_factor,
[email protected]3c0a3252013-03-18 04:24:36312 float min_page_scale_factor, float max_page_scale_factor) {
[email protected]d6021f6a2014-06-12 21:15:24313 SetPageScaleValues(page_scale_factor, min_page_scale_factor,
314 max_page_scale_factor, page_scale_delta_);
315}
[email protected]c60279472013-01-30 12:10:51316
[email protected]d6021f6a2014-06-12 21:15:24317void LayerTreeImpl::SetPageScaleDelta(float delta) {
318 SetPageScaleValues(page_scale_factor_, min_page_scale_factor_,
319 max_page_scale_factor_, delta);
320}
321
322void LayerTreeImpl::SetPageScaleValues(float page_scale_factor,
323 float min_page_scale_factor, float max_page_scale_factor,
324 float page_scale_delta) {
325 bool page_scale_changed =
326 min_page_scale_factor != min_page_scale_factor_ ||
327 max_page_scale_factor != max_page_scale_factor_ ||
328 page_scale_factor != page_scale_factor_;
[email protected]7265e74e2014-02-07 23:43:06329
[email protected]c60279472013-01-30 12:10:51330 min_page_scale_factor_ = min_page_scale_factor;
331 max_page_scale_factor_ = max_page_scale_factor;
332 page_scale_factor_ = page_scale_factor;
[email protected]20d2b742013-09-26 05:41:34333
[email protected]d6021f6a2014-06-12 21:15:24334 float total = page_scale_factor_ * page_scale_delta;
335 if (min_page_scale_factor_ && total < min_page_scale_factor_)
336 page_scale_delta = min_page_scale_factor_ / page_scale_factor_;
337 else if (max_page_scale_factor_ && total > max_page_scale_factor_)
338 page_scale_delta = max_page_scale_factor_ / page_scale_factor_;
339
340 if (page_scale_delta_ == page_scale_delta && !page_scale_changed)
341 return;
342
343 if (page_scale_delta_ != page_scale_delta) {
344 page_scale_delta_ = page_scale_delta;
345
346 if (IsActiveTree()) {
347 LayerTreeImpl* pending_tree = layer_tree_host_impl_->pending_tree();
348 if (pending_tree) {
349 DCHECK_EQ(1, pending_tree->sent_page_scale_delta());
350 pending_tree->SetPageScaleDelta(
351 page_scale_delta_ / sent_page_scale_delta_);
352 }
353 }
354
355 set_needs_update_draw_properties();
356 }
357
[email protected]22f200a2013-10-09 18:08:29358 if (root_layer_scroll_offset_delegate_) {
[email protected]ec2322e2014-05-15 16:32:00359 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
360 TotalScrollOffset(),
361 TotalMaxScrollOffset(),
362 ScrollableSize(),
[email protected]68fe60f2014-02-12 13:49:11363 total_page_scale_factor(),
[email protected]d6021f6a2014-06-12 21:15:24364 min_page_scale_factor_,
365 max_page_scale_factor_);
[email protected]22f200a2013-10-09 18:08:29366 }
[email protected]adeda572014-01-31 00:49:47367
368 ForceScrollbarParameterUpdateAfterScaleChange(page_scale_layer());
[email protected]c60279472013-01-30 12:10:51369}
370
[email protected]257abfa82013-01-29 23:47:24371gfx::SizeF LayerTreeImpl::ScrollableViewportSize() const {
[email protected]587941d2014-08-22 01:40:01372 if (!InnerViewportContainerLayer())
373 return gfx::SizeF();
374
bokanef971462014-10-13 22:58:32375 return gfx::ScaleSize(InnerViewportContainerLayer()->BoundsForScrolling(),
[email protected]587941d2014-08-22 01:40:01376 1.0f / total_page_scale_factor());
[email protected]257abfa82013-01-29 23:47:24377}
378
[email protected]3744e27b2013-11-06 21:44:08379gfx::Rect LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
[email protected]adeda572014-01-31 00:49:47380 LayerImpl* root_scroll_layer = OuterViewportScrollLayer()
381 ? OuterViewportScrollLayer()
382 : InnerViewportScrollLayer();
383 if (!root_scroll_layer || root_scroll_layer->children().empty())
[email protected]3744e27b2013-11-06 21:44:08384 return gfx::Rect();
[email protected]adeda572014-01-31 00:49:47385 LayerImpl* layer = root_scroll_layer->children()[0];
[email protected]8a822692014-02-12 17:30:55386 return MathUtil::MapEnclosingClippedRect(layer->screen_space_transform(),
387 gfx::Rect(layer->content_bounds()));
[email protected]3744e27b2013-11-06 21:44:08388}
389
[email protected]58241dc2013-08-20 01:39:25390static void ApplySentScrollDeltasFromAbortedCommitTo(LayerImpl* layer) {
391 layer->ApplySentScrollDeltasFromAbortedCommit();
[email protected]3519b872013-07-30 07:17:50392}
393
[email protected]58241dc2013-08-20 01:39:25394void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
[email protected]3519b872013-07-30 07:17:50395 DCHECK(IsActiveTree());
396
397 page_scale_factor_ *= sent_page_scale_delta_;
398 page_scale_delta_ /= sent_page_scale_delta_;
399 sent_page_scale_delta_ = 1.f;
400
bokan55b2f152014-09-15 14:47:59401 top_controls_content_offset_ += sent_top_controls_delta_;
bokan88eae012014-09-09 20:40:42402 top_controls_delta_ -= sent_top_controls_delta_;
403 sent_top_controls_delta_ = 0.f;
404
[email protected]3519b872013-07-30 07:17:50405 if (!root_layer())
406 return;
407
408 LayerTreeHostCommon::CallFunctionForSubtree(
[email protected]58241dc2013-08-20 01:39:25409 root_layer(), base::Bind(&ApplySentScrollDeltasFromAbortedCommitTo));
410}
411
[email protected]daea3d42013-10-23 17:04:50412static void ApplyScrollDeltasSinceBeginMainFrameTo(LayerImpl* layer) {
413 layer->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]58241dc2013-08-20 01:39:25414}
415
[email protected]daea3d42013-10-23 17:04:50416void LayerTreeImpl::ApplyScrollDeltasSinceBeginMainFrame() {
[email protected]58241dc2013-08-20 01:39:25417 DCHECK(IsPendingTree());
418 if (!root_layer())
419 return;
420
421 LayerTreeHostCommon::CallFunctionForSubtree(
[email protected]daea3d42013-10-23 17:04:50422 root_layer(), base::Bind(&ApplyScrollDeltasSinceBeginMainFrameTo));
[email protected]3519b872013-07-30 07:17:50423}
424
[email protected]57ac9482013-09-17 21:13:39425void LayerTreeImpl::SetViewportLayersFromIds(
426 int page_scale_layer_id,
427 int inner_viewport_scroll_layer_id,
428 int outer_viewport_scroll_layer_id) {
429 page_scale_layer_ = LayerById(page_scale_layer_id);
430 DCHECK(page_scale_layer_);
431
432 inner_viewport_scroll_layer_ =
433 LayerById(inner_viewport_scroll_layer_id);
434 DCHECK(inner_viewport_scroll_layer_);
435
436 outer_viewport_scroll_layer_ =
437 LayerById(outer_viewport_scroll_layer_id);
438 DCHECK(outer_viewport_scroll_layer_ ||
439 outer_viewport_scroll_layer_id == Layer::INVALID_ID);
[email protected]adeda572014-01-31 00:49:47440
441 if (!root_layer_scroll_offset_delegate_)
442 return;
443
444 inner_viewport_scroll_delegate_proxy_ = make_scoped_ptr(
445 new LayerScrollOffsetDelegateProxy(inner_viewport_scroll_layer_,
446 root_layer_scroll_offset_delegate_,
447 this));
448
449 if (outer_viewport_scroll_layer_)
450 outer_viewport_scroll_delegate_proxy_ = make_scoped_ptr(
451 new LayerScrollOffsetDelegateProxy(outer_viewport_scroll_layer_,
452 root_layer_scroll_offset_delegate_,
453 this));
[email protected]57ac9482013-09-17 21:13:39454}
455
456void LayerTreeImpl::ClearViewportLayers() {
457 page_scale_layer_ = NULL;
458 inner_viewport_scroll_layer_ = NULL;
459 outer_viewport_scroll_layer_ = NULL;
460}
461
[email protected]8f7f298822014-06-13 00:23:32462bool LayerTreeImpl::UpdateDrawProperties() {
463 if (!needs_update_draw_properties_)
464 return true;
[email protected]615c78a2013-01-24 23:44:16465
[email protected]ed511b8d2013-03-25 03:29:29466 // For max_texture_size.
[email protected]615c78a2013-01-24 23:44:16467 if (!layer_tree_host_impl_->renderer())
[email protected]8f7f298822014-06-13 00:23:32468 return false;
[email protected]615c78a2013-01-24 23:44:16469
[email protected]c1bb5af2013-03-13 19:06:27470 if (!root_layer())
[email protected]8f7f298822014-06-13 00:23:32471 return false;
472
473 needs_update_draw_properties_ = false;
474 render_surface_layer_list_.clear();
[email protected]76ffd9e2012-12-20 19:12:47475
[email protected]76ffd9e2012-12-20 19:12:47476 {
[email protected]7a52f43e2013-07-10 01:58:47477 TRACE_EVENT2("cc",
[email protected]c1bb5af2013-03-13 19:06:27478 "LayerTreeImpl::UpdateDrawProperties",
479 "IsActive",
[email protected]7a52f43e2013-07-10 01:58:47480 IsActiveTree(),
481 "SourceFrameNumber",
482 source_frame_number_);
[email protected]57ac9482013-09-17 21:13:39483 LayerImpl* page_scale_layer =
[email protected]fef74fd2014-02-27 06:28:17484 page_scale_layer_ ? page_scale_layer_ : InnerViewportContainerLayer();
hush367d7dd2014-08-29 23:56:01485 bool can_render_to_separate_surface =
486 (layer_tree_host_impl_->GetDrawMode() !=
487 DRAW_MODE_RESOURCELESS_SOFTWARE);
[email protected]390bb1ff2014-05-09 17:14:40488
489 ++render_surface_layer_list_id_;
[email protected]7aad55f2013-07-26 11:25:53490 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
[email protected]c1bb5af2013-03-13 19:06:27491 root_layer(),
[email protected]54af03522013-09-05 00:43:28492 DrawViewportSize(),
493 layer_tree_host_impl_->DrawTransform(),
[email protected]76ffd9e2012-12-20 19:12:47494 device_scale_factor(),
[email protected]c60279472013-01-30 12:10:51495 total_page_scale_factor(),
[email protected]57ac9482013-09-17 21:13:39496 page_scale_layer,
danakj132fdfe2014-09-07 06:08:16497 resource_provider()->max_texture_size(),
[email protected]8e0176d2013-03-21 03:14:52498 settings().can_use_lcd_text,
[email protected]45948712013-09-27 02:46:48499 can_render_to_separate_surface,
[email protected]35a99a12013-05-09 23:52:29500 settings().layer_transforms_should_scale_layer_contents,
[email protected]390bb1ff2014-05-09 17:14:40501 &render_surface_layer_list_,
502 render_surface_layer_list_id_);
[email protected]7aad55f2013-07-26 11:25:53503 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
[email protected]76ffd9e2012-12-20 19:12:47504 }
[email protected]615c78a2013-01-24 23:44:16505
[email protected]e4be0262013-10-19 16:54:28506 {
507 TRACE_EVENT2("cc",
508 "LayerTreeImpl::UpdateTilePriorities",
509 "IsActive",
510 IsActiveTree(),
511 "SourceFrameNumber",
512 source_frame_number_);
danakj6496cba2014-10-16 01:31:08513 scoped_ptr<OcclusionTracker<LayerImpl>> occlusion_tracker;
[email protected]562b7ad2014-06-23 22:17:11514 if (settings().use_occlusion_for_tile_prioritization) {
515 occlusion_tracker.reset(new OcclusionTracker<LayerImpl>(
516 root_layer()->render_surface()->content_rect()));
517 occlusion_tracker->set_minimum_tracking_size(
518 settings().minimum_occlusion_tracking_size);
519 }
520
boliu7473f7f52014-10-01 16:54:56521 bool resourceless_software_draw = (layer_tree_host_impl_->GetDrawMode() ==
522 DRAW_MODE_RESOURCELESS_SOFTWARE);
523
[email protected]e4be0262013-10-19 16:54:28524 // LayerIterator is used here instead of CallFunctionForSubtree to only
525 // UpdateTilePriorities on layers that will be visible (and thus have valid
526 // draw properties) and not because any ordering is required.
[email protected]ba1b33e2014-02-28 16:44:51527 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]e4be0262013-10-19 16:54:28528 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
529 for (LayerIteratorType it =
530 LayerIteratorType::Begin(&render_surface_layer_list_);
531 it != end;
532 ++it) {
[email protected]562b7ad2014-06-23 22:17:11533 if (occlusion_tracker)
534 occlusion_tracker->EnterLayer(it);
535
[email protected]e4be0262013-10-19 16:54:28536 LayerImpl* layer = *it;
vmpstrcdcb5f72014-09-11 00:58:37537 const Occlusion& occlusion_in_content_space =
538 occlusion_tracker ? occlusion_tracker->GetCurrentOcclusionForLayer(
539 layer->draw_transform())
540 : Occlusion();
541
boliu7473f7f52014-10-01 16:54:56542 if (it.represents_itself()) {
543 layer->UpdateTiles(occlusion_in_content_space,
544 resourceless_software_draw);
545 }
[email protected]e4be0262013-10-19 16:54:28546
[email protected]562b7ad2014-06-23 22:17:11547 if (!it.represents_contributing_render_surface()) {
548 if (occlusion_tracker)
549 occlusion_tracker->LeaveLayer(it);
[email protected]6355d2d2014-05-07 15:07:27550 continue;
[email protected]562b7ad2014-06-23 22:17:11551 }
[email protected]6355d2d2014-05-07 15:07:27552
boliu7473f7f52014-10-01 16:54:56553 if (layer->mask_layer()) {
554 layer->mask_layer()->UpdateTiles(occlusion_in_content_space,
555 resourceless_software_draw);
556 }
557 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
[email protected]562b7ad2014-06-23 22:17:11558 layer->replica_layer()->mask_layer()->UpdateTiles(
boliu7473f7f52014-10-01 16:54:56559 occlusion_in_content_space, resourceless_software_draw);
560 }
[email protected]562b7ad2014-06-23 22:17:11561
562 if (occlusion_tracker)
563 occlusion_tracker->LeaveLayer(it);
[email protected]e4be0262013-10-19 16:54:28564 }
565 }
566
[email protected]615c78a2013-01-24 23:44:16567 DCHECK(!needs_update_draw_properties_) <<
[email protected]7d19dc342013-05-02 22:02:04568 "CalcDrawProperties should not set_needs_update_draw_properties()";
[email protected]8f7f298822014-06-13 00:23:32569 return true;
[email protected]76ffd9e2012-12-20 19:12:47570}
571
[email protected]50761e92013-03-29 20:51:28572const LayerImplList& LayerTreeImpl::RenderSurfaceLayerList() const {
[email protected]76ffd9e2012-12-20 19:12:47573 // If this assert triggers, then the list is dirty.
[email protected]615c78a2013-01-24 23:44:16574 DCHECK(!needs_update_draw_properties_);
[email protected]76ffd9e2012-12-20 19:12:47575 return render_surface_layer_list_;
576}
577
bokancccfde72014-10-08 15:15:22578gfx::Size LayerTreeImpl::ScrollableSize() const {
[email protected]adeda572014-01-31 00:49:47579 LayerImpl* root_scroll_layer = OuterViewportScrollLayer()
580 ? OuterViewportScrollLayer()
581 : InnerViewportScrollLayer();
582 if (!root_scroll_layer || root_scroll_layer->children().empty())
bokancccfde72014-10-08 15:15:22583 return gfx::Size();
[email protected]adeda572014-01-31 00:49:47584 return root_scroll_layer->children()[0]->bounds();
[email protected]caa567d2012-12-20 07:56:16585}
586
[email protected]361bc00d2012-12-14 07:03:24587LayerImpl* LayerTreeImpl::LayerById(int id) {
588 LayerIdMap::iterator iter = layer_id_map_.find(id);
589 return iter != layer_id_map_.end() ? iter->second : NULL;
590}
591
592void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
593 DCHECK(!LayerById(layer->id()));
594 layer_id_map_[layer->id()] = layer;
595}
596
597void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
598 DCHECK(LayerById(layer->id()));
599 layer_id_map_.erase(layer->id());
600}
601
[email protected]aebf4622014-07-14 16:57:59602size_t LayerTreeImpl::NumLayers() {
603 return layer_id_map_.size();
604}
605
[email protected]ed511b8d2013-03-25 03:29:29606void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pending_tree) {
[email protected]a90fac72013-06-06 18:56:13607 pending_tree->SetCurrentlyScrollingLayer(
608 LayerTreeHostCommon::FindLayerInSubtree(pending_tree->root_layer(),
609 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0));
[email protected]1e0f8d62013-01-09 07:41:35610}
611
[email protected]ff1211d2013-06-07 01:58:35612static void DidBecomeActiveRecursive(LayerImpl* layer) {
[email protected]7aba6662013-03-12 10:17:34613 layer->DidBecomeActive();
[email protected]db2e29c2014-08-06 05:58:25614 if (layer->mask_layer())
615 layer->mask_layer()->DidBecomeActive();
616 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
617 layer->replica_layer()->mask_layer()->DidBecomeActive();
618
[email protected]ff1211d2013-06-07 01:58:35619 for (size_t i = 0; i < layer->children().size(); ++i)
620 DidBecomeActiveRecursive(layer->children()[i]);
[email protected]37386f052013-01-13 00:42:22621}
622
623void LayerTreeImpl::DidBecomeActive() {
[email protected]12a63da2014-06-13 06:06:22624 if (next_activation_forces_redraw_) {
625 layer_tree_host_impl_->SetFullRootLayerDamage();
626 next_activation_forces_redraw_ = false;
627 }
628
[email protected]adeda572014-01-31 00:49:47629 if (scrolling_layer_id_from_previous_tree_) {
630 currently_scrolling_layer_ = LayerTreeHostCommon::FindLayerInSubtree(
[email protected]7dcf5632014-06-25 01:11:55631 root_layer(), scrolling_layer_id_from_previous_tree_);
[email protected]adeda572014-01-31 00:49:47632 }
633
[email protected]7dcf5632014-06-25 01:11:55634 // Always reset this flag on activation, as we would only have activated
635 // if we were in a good state.
vmpstr61ed94a12014-10-09 04:49:30636 layer_tree_host_impl_->ResetRequiresHighResToDraw();
[email protected]7dcf5632014-06-25 01:11:55637
638 if (root_layer())
639 DidBecomeActiveRecursive(root_layer());
640
[email protected]12a63da2014-06-13 06:06:22641 devtools_instrumentation::DidActivateLayerTree(layer_tree_host_impl_->id(),
642 source_frame_number_);
[email protected]37386f052013-01-13 00:42:22643}
644
[email protected]6f90b9e2013-01-17 23:42:00645bool LayerTreeImpl::ContentsTexturesPurged() const {
646 return contents_textures_purged_;
647}
648
649void LayerTreeImpl::SetContentsTexturesPurged() {
[email protected]94bf75c2013-06-12 13:20:04650 if (contents_textures_purged_)
651 return;
[email protected]6f90b9e2013-01-17 23:42:00652 contents_textures_purged_ = true;
[email protected]c1bb5af2013-03-13 19:06:27653 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]6f90b9e2013-01-17 23:42:00654}
655
656void LayerTreeImpl::ResetContentsTexturesPurged() {
[email protected]94bf75c2013-06-12 13:20:04657 if (!contents_textures_purged_)
658 return;
[email protected]6f90b9e2013-01-17 23:42:00659 contents_textures_purged_ = false;
[email protected]c1bb5af2013-03-13 19:06:27660 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]6f90b9e2013-01-17 23:42:00661}
662
[email protected]3d609bb2014-02-01 01:10:23663bool LayerTreeImpl::RequiresHighResToDraw() const {
vmpstr61ed94a12014-10-09 04:49:30664 return layer_tree_host_impl_->RequiresHighResToDraw();
[email protected]3d609bb2014-02-01 01:10:23665}
666
[email protected]318822852013-02-14 00:54:27667bool LayerTreeImpl::ViewportSizeInvalid() const {
668 return viewport_size_invalid_;
669}
670
671void LayerTreeImpl::SetViewportSizeInvalid() {
672 viewport_size_invalid_ = true;
[email protected]c1bb5af2013-03-13 19:06:27673 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]318822852013-02-14 00:54:27674}
675
676void LayerTreeImpl::ResetViewportSizeInvalid() {
677 viewport_size_invalid_ = false;
[email protected]c1bb5af2013-03-13 19:06:27678 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
[email protected]318822852013-02-14 00:54:27679}
680
[email protected]48871fc2013-01-23 07:36:51681Proxy* LayerTreeImpl::proxy() const {
682 return layer_tree_host_impl_->proxy();
683}
684
[email protected]ff762fb2012-12-12 19:18:37685const LayerTreeSettings& LayerTreeImpl::settings() const {
[email protected]c1bb5af2013-03-13 19:06:27686 return layer_tree_host_impl_->settings();
[email protected]ff762fb2012-12-12 19:18:37687}
688
[email protected]7a8bcd262014-01-15 12:54:58689const RendererCapabilitiesImpl& LayerTreeImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:27690 return layer_tree_host_impl_->GetRendererCapabilities();
[email protected]bf5b3a02013-02-13 02:02:52691}
692
[email protected]0634cdd42013-08-16 00:46:09693ContextProvider* LayerTreeImpl::context_provider() const {
dcheng6afa17002014-08-26 19:11:31694 return output_surface()->context_provider();
[email protected]0634cdd42013-08-16 00:46:09695}
696
[email protected]ff762fb2012-12-12 19:18:37697OutputSurface* LayerTreeImpl::output_surface() const {
[email protected]c1bb5af2013-03-13 19:06:27698 return layer_tree_host_impl_->output_surface();
[email protected]ff762fb2012-12-12 19:18:37699}
700
701ResourceProvider* LayerTreeImpl::resource_provider() const {
[email protected]c1bb5af2013-03-13 19:06:27702 return layer_tree_host_impl_->resource_provider();
[email protected]ff762fb2012-12-12 19:18:37703}
704
705TileManager* LayerTreeImpl::tile_manager() const {
[email protected]c1bb5af2013-03-13 19:06:27706 return layer_tree_host_impl_->tile_manager();
[email protected]ff762fb2012-12-12 19:18:37707}
708
709FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
[email protected]c1bb5af2013-03-13 19:06:27710 return layer_tree_host_impl_->fps_counter();
[email protected]ff762fb2012-12-12 19:18:37711}
712
[email protected]71691c22013-01-18 03:14:22713PaintTimeCounter* LayerTreeImpl::paint_time_counter() const {
[email protected]c1bb5af2013-03-13 19:06:27714 return layer_tree_host_impl_->paint_time_counter();
[email protected]71691c22013-01-18 03:14:22715}
716
[email protected]1191d9d2013-02-02 06:00:33717MemoryHistory* LayerTreeImpl::memory_history() const {
[email protected]c1bb5af2013-03-13 19:06:27718 return layer_tree_host_impl_->memory_history();
[email protected]1191d9d2013-02-02 06:00:33719}
720
[email protected]4a6c091d2014-04-24 21:06:46721gfx::Size LayerTreeImpl::device_viewport_size() const {
722 return layer_tree_host_impl_->device_viewport_size();
723}
724
[email protected]f117a4c2012-12-16 04:53:10725bool LayerTreeImpl::IsActiveTree() const {
[email protected]c1bb5af2013-03-13 19:06:27726 return layer_tree_host_impl_->active_tree() == this;
[email protected]f117a4c2012-12-16 04:53:10727}
728
729bool LayerTreeImpl::IsPendingTree() const {
[email protected]c1bb5af2013-03-13 19:06:27730 return layer_tree_host_impl_->pending_tree() == this;
[email protected]f117a4c2012-12-16 04:53:10731}
732
[email protected]48871fc2013-01-23 07:36:51733bool LayerTreeImpl::IsRecycleTree() const {
[email protected]c1bb5af2013-03-13 19:06:27734 return layer_tree_host_impl_->recycle_tree() == this;
[email protected]48871fc2013-01-23 07:36:51735}
736
[email protected]f117a4c2012-12-16 04:53:10737LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
[email protected]c1bb5af2013-03-13 19:06:27738 LayerTreeImpl* tree = layer_tree_host_impl_->active_tree();
[email protected]f117a4c2012-12-16 04:53:10739 if (!tree)
740 return NULL;
741 return tree->LayerById(id);
742}
743
744LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
[email protected]c1bb5af2013-03-13 19:06:27745 LayerTreeImpl* tree = layer_tree_host_impl_->pending_tree();
[email protected]f117a4c2012-12-16 04:53:10746 if (!tree)
747 return NULL;
748 return tree->LayerById(id);
749}
750
[email protected]71618ed2014-07-24 02:23:45751LayerImpl* LayerTreeImpl::FindRecycleTreeLayerById(int id) {
752 LayerTreeImpl* tree = layer_tree_host_impl_->recycle_tree();
753 if (!tree)
754 return NULL;
755 return tree->LayerById(id);
756}
757
[email protected]166db5c82013-01-09 23:54:31758bool LayerTreeImpl::PinchGestureActive() const {
[email protected]c1bb5af2013-03-13 19:06:27759 return layer_tree_host_impl_->pinch_gesture_active();
[email protected]166db5c82013-01-09 23:54:31760}
761
[email protected]04c5900d2014-08-18 13:38:36762BeginFrameArgs LayerTreeImpl::CurrentBeginFrameArgs() const {
763 return layer_tree_host_impl_->CurrentBeginFrameArgs();
[email protected]fb7425a2013-04-22 16:28:55764}
765
[email protected]c92195e2014-05-07 18:18:49766base::TimeDelta LayerTreeImpl::begin_impl_frame_interval() const {
767 return layer_tree_host_impl_->begin_impl_frame_interval();
768}
769
[email protected]d7eb8c72013-03-23 22:57:13770void LayerTreeImpl::SetNeedsCommit() {
771 layer_tree_host_impl_->SetNeedsCommit();
772}
773
[email protected]bd5324592014-07-31 09:09:33774gfx::Rect LayerTreeImpl::DeviceViewport() const {
775 return layer_tree_host_impl_->DeviceViewport();
776}
777
[email protected]54af03522013-09-05 00:43:28778gfx::Size LayerTreeImpl::DrawViewportSize() const {
779 return layer_tree_host_impl_->DrawViewportSize();
780}
781
[email protected]bd5324592014-07-31 09:09:33782const gfx::Rect LayerTreeImpl::ViewportRectForTilePriority() const {
783 return layer_tree_host_impl_->ViewportRectForTilePriority();
784}
785
[email protected]930ff43b2014-05-02 05:24:00786scoped_ptr<ScrollbarAnimationController>
787LayerTreeImpl::CreateScrollbarAnimationController(LayerImpl* scrolling_layer) {
788 DCHECK(settings().scrollbar_fade_delay_ms);
789 DCHECK(settings().scrollbar_fade_duration_ms);
790 base::TimeDelta delay =
791 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_delay_ms);
sataya.m07f11a82014-10-07 14:29:18792 base::TimeDelta resize_delay = base::TimeDelta::FromMilliseconds(
793 settings().scrollbar_fade_resize_delay_ms);
[email protected]930ff43b2014-05-02 05:24:00794 base::TimeDelta duration =
795 base::TimeDelta::FromMilliseconds(settings().scrollbar_fade_duration_ms);
796 switch (settings().scrollbar_animator) {
797 case LayerTreeSettings::LinearFade: {
798 return ScrollbarAnimationControllerLinearFade::Create(
sataya.m07f11a82014-10-07 14:29:18799 scrolling_layer,
800 layer_tree_host_impl_,
801 delay,
802 resize_delay,
803 duration);
[email protected]930ff43b2014-05-02 05:24:00804 }
805 case LayerTreeSettings::Thinning: {
sataya.m07f11a82014-10-07 14:29:18806 return ScrollbarAnimationControllerThinning::Create(scrolling_layer,
807 layer_tree_host_impl_,
808 delay,
809 resize_delay,
810 duration);
[email protected]930ff43b2014-05-02 05:24:00811 }
812 case LayerTreeSettings::NoAnimator:
813 NOTREACHED();
814 break;
815 }
danakjf446a072014-09-27 21:55:48816 return nullptr;
[email protected]2ea5aba2013-09-11 14:26:56817}
818
[email protected]b8384e22013-12-03 02:20:48819void LayerTreeImpl::DidAnimateScrollOffset() {
820 layer_tree_host_impl_->DidAnimateScrollOffset();
821}
822
[email protected]13525d62014-05-20 21:22:04823bool LayerTreeImpl::use_gpu_rasterization() const {
824 return layer_tree_host_impl_->use_gpu_rasterization();
825}
826
[email protected]473f1f22014-05-22 08:19:17827bool LayerTreeImpl::create_low_res_tiling() const {
828 return layer_tree_host_impl_->create_low_res_tiling();
829}
830
[email protected]ff762fb2012-12-12 19:18:37831void LayerTreeImpl::SetNeedsRedraw() {
[email protected]59adb112013-04-09 04:48:44832 layer_tree_host_impl_->SetNeedsRedraw();
[email protected]ff762fb2012-12-12 19:18:37833}
834
[email protected]ff762fb2012-12-12 19:18:37835const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
[email protected]c1bb5af2013-03-13 19:06:27836 return layer_tree_host_impl_->debug_state();
[email protected]ff762fb2012-12-12 19:18:37837}
838
839float LayerTreeImpl::device_scale_factor() const {
[email protected]c1bb5af2013-03-13 19:06:27840 return layer_tree_host_impl_->device_scale_factor();
[email protected]ff762fb2012-12-12 19:18:37841}
842
[email protected]ff762fb2012-12-12 19:18:37843DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
[email protected]c1bb5af2013-03-13 19:06:27844 return layer_tree_host_impl_->debug_rect_history();
[email protected]ff762fb2012-12-12 19:18:37845}
846
[email protected]de4afb5e2012-12-20 00:11:34847AnimationRegistrar* LayerTreeImpl::animationRegistrar() const {
[email protected]c1bb5af2013-03-13 19:06:27848 return layer_tree_host_impl_->animation_registrar();
[email protected]de4afb5e2012-12-20 00:11:34849}
[email protected]ff762fb2012-12-12 19:18:37850
vmpstrd7de03c2014-08-27 18:11:01851void LayerTreeImpl::GetAllTilesForTracing(std::set<const Tile*>* tiles) const {
852 typedef LayerIterator<LayerImpl> LayerIteratorType;
853 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
854 for (LayerIteratorType it =
855 LayerIteratorType::Begin(&render_surface_layer_list_);
856 it != end;
857 ++it) {
858 if (!it.represents_itself())
859 continue;
860 LayerImpl* layer_impl = *it;
861 layer_impl->GetAllTilesForTracing(tiles);
862 }
863}
864
[email protected]d12aa932014-08-01 13:10:38865void LayerTreeImpl::AsValueInto(base::debug::TracedValue* state) const {
866 TracedValue::MakeDictIntoImplicitSnapshot(state, "cc::LayerTreeImpl", this);
nduca929378a02014-08-23 19:48:52867 state->SetInteger("source_frame_number", source_frame_number_);
[email protected]f6742f52013-05-08 23:52:22868
[email protected]d12aa932014-08-01 13:10:38869 state->BeginDictionary("root_layer");
870 root_layer_->AsValueInto(state);
871 state->EndDictionary();
[email protected]f6742f52013-05-08 23:52:22872
[email protected]d12aa932014-08-01 13:10:38873 state->BeginArray("render_surface_layer_list");
[email protected]ba1b33e2014-02-28 16:44:51874 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]71dfcc72013-03-20 21:30:09875 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
876 for (LayerIteratorType it = LayerIteratorType::Begin(
[email protected]8c5690222013-02-15 17:36:43877 &render_surface_layer_list_); it != end; ++it) {
[email protected]71dfcc72013-03-20 21:30:09878 if (!it.represents_itself())
[email protected]8c5690222013-02-15 17:36:43879 continue;
[email protected]d12aa932014-08-01 13:10:38880 TracedValue::AppendIDRef(*it, state);
[email protected]8c5690222013-02-15 17:36:43881 }
[email protected]d12aa932014-08-01 13:10:38882 state->EndArray();
skyostil43c330f72014-09-22 16:49:11883
884 state->BeginArray("swap_promise_trace_ids");
885 for (size_t i = 0; i < swap_promise_list_.size(); i++)
886 state->AppendDouble(swap_promise_list_[i]->TraceId());
887 state->EndArray();
[email protected]8c5690222013-02-15 17:36:43888}
889
[email protected]1960a712013-04-30 17:06:47890void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
[email protected]c9280762013-08-01 06:28:57891 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
[email protected]20d2b742013-09-26 05:41:34892 if (root_layer_scroll_offset_delegate_ == root_layer_scroll_offset_delegate)
893 return;
894
[email protected]adeda572014-01-31 00:49:47895 if (!root_layer_scroll_offset_delegate) {
896 // Make sure we remove the proxies from their layers before
897 // releasing them.
898 if (InnerViewportScrollLayer())
899 InnerViewportScrollLayer()->SetScrollOffsetDelegate(NULL);
900 if (OuterViewportScrollLayer())
901 OuterViewportScrollLayer()->SetScrollOffsetDelegate(NULL);
danakjf446a072014-09-27 21:55:48902 inner_viewport_scroll_delegate_proxy_ = nullptr;
903 outer_viewport_scroll_delegate_proxy_ = nullptr;
[email protected]d35cd7b22014-01-29 14:32:46904 }
905
[email protected]adeda572014-01-31 00:49:47906 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
907
[email protected]20d2b742013-09-26 05:41:34908 if (root_layer_scroll_offset_delegate_) {
[email protected]ec2322e2014-05-15 16:32:00909 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
910 TotalScrollOffset(),
911 TotalMaxScrollOffset(),
912 ScrollableSize(),
[email protected]68fe60f2014-02-12 13:49:11913 total_page_scale_factor(),
914 min_page_scale_factor(),
915 max_page_scale_factor());
[email protected]adeda572014-01-31 00:49:47916
917 if (inner_viewport_scroll_layer_) {
918 inner_viewport_scroll_delegate_proxy_ = make_scoped_ptr(
919 new LayerScrollOffsetDelegateProxy(InnerViewportScrollLayer(),
920 root_layer_scroll_offset_delegate_,
921 this));
922 inner_viewport_scroll_layer_->SetScrollOffsetDelegate(
923 inner_viewport_scroll_delegate_proxy_.get());
924 }
925
926 if (outer_viewport_scroll_layer_) {
927 outer_viewport_scroll_delegate_proxy_ = make_scoped_ptr(
928 new LayerScrollOffsetDelegateProxy(OuterViewportScrollLayer(),
929 root_layer_scroll_offset_delegate_,
930 this));
931 outer_viewport_scroll_layer_->SetScrollOffsetDelegate(
932 outer_viewport_scroll_delegate_proxy_.get());
933 }
[email protected]20d2b742013-09-26 05:41:34934 }
[email protected]1960a712013-04-30 17:06:47935}
936
boliu7d5dbab2014-10-10 20:05:47937void LayerTreeImpl::OnRootLayerDelegatedScrollOffsetChanged() {
938 DCHECK(root_layer_scroll_offset_delegate_);
939 if (inner_viewport_scroll_layer_) {
940 inner_viewport_scroll_layer_->DidScroll();
941 }
942 if (outer_viewport_scroll_layer_) {
943 outer_viewport_scroll_layer_->DidScroll();
944 }
945}
946
[email protected]adeda572014-01-31 00:49:47947void LayerTreeImpl::UpdateScrollOffsetDelegate() {
948 DCHECK(InnerViewportScrollLayer());
949 DCHECK(root_layer_scroll_offset_delegate_);
950
miletusf57925d2014-10-01 19:38:13951 gfx::ScrollOffset offset =
[email protected]adeda572014-01-31 00:49:47952 inner_viewport_scroll_delegate_proxy_->last_set_scroll_offset();
953
954 if (OuterViewportScrollLayer())
955 offset += outer_viewport_scroll_delegate_proxy_->last_set_scroll_offset();
956
[email protected]ec2322e2014-05-15 16:32:00957 root_layer_scroll_offset_delegate_->UpdateRootLayerState(
958 offset,
959 TotalMaxScrollOffset(),
960 ScrollableSize(),
961 total_page_scale_factor(),
962 min_page_scale_factor(),
963 max_page_scale_factor());
[email protected]adeda572014-01-31 00:49:47964}
965
miletusf57925d2014-10-01 19:38:13966gfx::ScrollOffset LayerTreeImpl::GetDelegatedScrollOffset(LayerImpl* layer) {
[email protected]adeda572014-01-31 00:49:47967 DCHECK(root_layer_scroll_offset_delegate_);
968 DCHECK(InnerViewportScrollLayer());
969 if (layer == InnerViewportScrollLayer() && !OuterViewportScrollLayer())
970 return root_layer_scroll_offset_delegate_->GetTotalScrollOffset();
971
972 // If we get here, we have both inner/outer viewports, and need to distribute
973 // the scroll offset between them.
974 DCHECK(inner_viewport_scroll_delegate_proxy_);
975 DCHECK(outer_viewport_scroll_delegate_proxy_);
miletusf57925d2014-10-01 19:38:13976 gfx::ScrollOffset inner_viewport_offset =
[email protected]adeda572014-01-31 00:49:47977 inner_viewport_scroll_delegate_proxy_->last_set_scroll_offset();
miletusf57925d2014-10-01 19:38:13978 gfx::ScrollOffset outer_viewport_offset =
[email protected]adeda572014-01-31 00:49:47979 outer_viewport_scroll_delegate_proxy_->last_set_scroll_offset();
980
981 // It may be nothing has changed.
miletusf57925d2014-10-01 19:38:13982 gfx::ScrollOffset delegate_offset =
[email protected]adeda572014-01-31 00:49:47983 root_layer_scroll_offset_delegate_->GetTotalScrollOffset();
984 if (inner_viewport_offset + outer_viewport_offset == delegate_offset) {
985 if (layer == InnerViewportScrollLayer())
986 return inner_viewport_offset;
987 else
988 return outer_viewport_offset;
989 }
990
miletusf57925d2014-10-01 19:38:13991 gfx::ScrollOffset max_outer_viewport_scroll_offset =
[email protected]adeda572014-01-31 00:49:47992 OuterViewportScrollLayer()->MaxScrollOffset();
993
994 outer_viewport_offset = delegate_offset - inner_viewport_offset;
995 outer_viewport_offset.SetToMin(max_outer_viewport_scroll_offset);
miletusf57925d2014-10-01 19:38:13996 outer_viewport_offset.SetToMax(gfx::ScrollOffset());
[email protected]adeda572014-01-31 00:49:47997
998 if (layer == OuterViewportScrollLayer())
999 return outer_viewport_offset;
1000
1001 inner_viewport_offset = delegate_offset - outer_viewport_offset;
1002
1003 return inner_viewport_offset;
1004}
1005
[email protected]b69c1db2013-11-27 00:05:191006void LayerTreeImpl::QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise) {
1007 DCHECK(swap_promise);
[email protected]b69c1db2013-11-27 00:05:191008 swap_promise_list_.push_back(swap_promise.Pass());
1009}
1010
1011void LayerTreeImpl::PassSwapPromises(
1012 ScopedPtrVector<SwapPromise>* new_swap_promise) {
1013 swap_promise_list_.insert_and_take(swap_promise_list_.end(),
weiliangcc1878c62014-09-02 22:43:171014 new_swap_promise);
[email protected]b69c1db2013-11-27 00:05:191015 new_swap_promise->clear();
1016}
1017
[email protected]d359203a2013-11-29 06:16:551018void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata* metadata) {
[email protected]b69c1db2013-11-27 00:05:191019 for (size_t i = 0; i < swap_promise_list_.size(); i++)
[email protected]d359203a2013-11-29 06:16:551020 swap_promise_list_[i]->DidSwap(metadata);
[email protected]b69c1db2013-11-27 00:05:191021 swap_promise_list_.clear();
1022}
1023
1024void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason) {
1025 for (size_t i = 0; i < swap_promise_list_.size(); i++)
1026 swap_promise_list_[i]->DidNotSwap(reason);
1027 swap_promise_list_.clear();
1028}
1029
[email protected]c48536a52013-09-14 00:02:081030void LayerTreeImpl::DidModifyTilePriorities() {
1031 layer_tree_host_impl_->DidModifyTilePriorities();
[email protected]fcb846d2013-05-22 01:42:361032}
1033
[email protected]c9280762013-08-01 06:28:571034void LayerTreeImpl::set_ui_resource_request_queue(
1035 const UIResourceRequestQueue& queue) {
1036 ui_resource_request_queue_ = queue;
1037}
1038
1039ResourceProvider::ResourceId LayerTreeImpl::ResourceIdForUIResource(
1040 UIResourceId uid) const {
1041 return layer_tree_host_impl_->ResourceIdForUIResource(uid);
1042}
1043
[email protected]709c9542013-10-26 01:43:511044bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid) const {
1045 return layer_tree_host_impl_->IsUIResourceOpaque(uid);
1046}
1047
[email protected]c9280762013-08-01 06:28:571048void LayerTreeImpl::ProcessUIResourceRequestQueue() {
1049 while (ui_resource_request_queue_.size() > 0) {
1050 UIResourceRequest req = ui_resource_request_queue_.front();
1051 ui_resource_request_queue_.pop_front();
1052
[email protected]741fba422013-09-20 03:34:141053 switch (req.GetType()) {
[email protected]c9280762013-08-01 06:28:571054 case UIResourceRequest::UIResourceCreate:
[email protected]741fba422013-09-20 03:34:141055 layer_tree_host_impl_->CreateUIResource(req.GetId(), req.GetBitmap());
[email protected]c9280762013-08-01 06:28:571056 break;
1057 case UIResourceRequest::UIResourceDelete:
[email protected]741fba422013-09-20 03:34:141058 layer_tree_host_impl_->DeleteUIResource(req.GetId());
[email protected]c9280762013-08-01 06:28:571059 break;
[email protected]f28d64d2013-08-27 04:17:451060 case UIResourceRequest::UIResourceInvalidRequest:
[email protected]c9280762013-08-01 06:28:571061 NOTREACHED();
1062 break;
1063 }
1064 }
[email protected]127bdc1a2013-09-11 01:44:481065
1066 // If all UI resource evictions were not recreated by processing this queue,
1067 // then another commit is required.
1068 if (layer_tree_host_impl_->EvictedUIResourcesExist())
1069 layer_tree_host_impl_->SetNeedsCommit();
[email protected]c9280762013-08-01 06:28:571070}
1071
[email protected]30fe19ff2013-07-04 00:54:451072void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl* layer) {
1073 // Only the active tree needs to know about layers with copy requests, as
1074 // they are aborted if not serviced during draw.
1075 DCHECK(IsActiveTree());
1076
[email protected]a4ee12812014-02-06 17:33:381077 // DCHECK(std::find(layers_with_copy_output_request_.begin(),
1078 // layers_with_copy_output_request_.end(),
1079 // layer) == layers_with_copy_output_request_.end());
1080 // TODO(danakj): Remove this once crash is found crbug.com/309777
1081 for (size_t i = 0; i < layers_with_copy_output_request_.size(); ++i) {
1082 CHECK(layers_with_copy_output_request_[i] != layer)
1083 << i << " of " << layers_with_copy_output_request_.size();
1084 }
[email protected]30fe19ff2013-07-04 00:54:451085 layers_with_copy_output_request_.push_back(layer);
1086}
1087
1088void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl* layer) {
1089 // Only the active tree needs to know about layers with copy requests, as
1090 // they are aborted if not serviced during draw.
1091 DCHECK(IsActiveTree());
1092
1093 std::vector<LayerImpl*>::iterator it = std::find(
1094 layers_with_copy_output_request_.begin(),
1095 layers_with_copy_output_request_.end(),
1096 layer);
1097 DCHECK(it != layers_with_copy_output_request_.end());
[email protected]f5de9e5b2013-07-30 22:26:571098 layers_with_copy_output_request_.erase(it);
[email protected]a4ee12812014-02-06 17:33:381099
1100 // TODO(danakj): Remove this once crash is found crbug.com/309777
1101 for (size_t i = 0; i < layers_with_copy_output_request_.size(); ++i) {
1102 CHECK(layers_with_copy_output_request_[i] != layer)
1103 << i << " of " << layers_with_copy_output_request_.size();
1104 }
[email protected]30fe19ff2013-07-04 00:54:451105}
1106
[email protected]53526372013-12-07 04:31:501107const std::vector<LayerImpl*>& LayerTreeImpl::LayersWithCopyOutputRequest()
[email protected]30fe19ff2013-07-04 00:54:451108 const {
1109 // Only the active tree needs to know about layers with copy requests, as
1110 // they are aborted if not serviced during draw.
1111 DCHECK(IsActiveTree());
1112
1113 return layers_with_copy_output_request_;
1114}
1115
[email protected]aeef2f02014-05-10 12:15:481116void LayerTreeImpl::ReleaseResourcesRecursive(LayerImpl* current) {
1117 DCHECK(current);
1118 current->ReleaseResources();
1119 if (current->mask_layer())
1120 ReleaseResourcesRecursive(current->mask_layer());
1121 if (current->replica_layer())
1122 ReleaseResourcesRecursive(current->replica_layer());
1123 for (size_t i = 0; i < current->children().size(); ++i)
1124 ReleaseResourcesRecursive(current->children()[i]);
1125}
1126
[email protected]28336d52014-05-12 19:07:281127template <typename LayerType>
1128static inline bool LayerClipsSubtree(LayerType* layer) {
1129 return layer->masks_to_bounds() || layer->mask_layer();
1130}
1131
1132static bool PointHitsRect(
1133 const gfx::PointF& screen_space_point,
1134 const gfx::Transform& local_space_to_screen_space_transform,
1135 const gfx::RectF& local_space_rect,
1136 float* distance_to_camera) {
1137 // If the transform is not invertible, then assume that this point doesn't hit
1138 // this rect.
1139 gfx::Transform inverse_local_space_to_screen_space(
1140 gfx::Transform::kSkipInitialization);
1141 if (!local_space_to_screen_space_transform.GetInverse(
1142 &inverse_local_space_to_screen_space))
1143 return false;
1144
1145 // Transform the hit test point from screen space to the local space of the
1146 // given rect.
1147 bool clipped = false;
1148 gfx::Point3F planar_point = MathUtil::ProjectPoint3D(
1149 inverse_local_space_to_screen_space, screen_space_point, &clipped);
1150 gfx::PointF hit_test_point_in_local_space =
1151 gfx::PointF(planar_point.x(), planar_point.y());
1152
1153 // If ProjectPoint could not project to a valid value, then we assume that
1154 // this point doesn't hit this rect.
1155 if (clipped)
1156 return false;
1157
1158 if (!local_space_rect.Contains(hit_test_point_in_local_space))
1159 return false;
1160
1161 if (distance_to_camera) {
1162 // To compute the distance to the camera, we have to take the planar point
1163 // and pull it back to world space and compute the displacement along the
1164 // z-axis.
1165 gfx::Point3F planar_point_in_screen_space(planar_point);
1166 local_space_to_screen_space_transform.TransformPoint(
1167 &planar_point_in_screen_space);
1168 *distance_to_camera = planar_point_in_screen_space.z();
1169 }
1170
1171 return true;
1172}
1173
1174static bool PointHitsRegion(const gfx::PointF& screen_space_point,
1175 const gfx::Transform& screen_space_transform,
1176 const Region& layer_space_region,
1177 float layer_content_scale_x,
1178 float layer_content_scale_y) {
1179 // If the transform is not invertible, then assume that this point doesn't hit
1180 // this region.
1181 gfx::Transform inverse_screen_space_transform(
1182 gfx::Transform::kSkipInitialization);
1183 if (!screen_space_transform.GetInverse(&inverse_screen_space_transform))
1184 return false;
1185
1186 // Transform the hit test point from screen space to the local space of the
1187 // given region.
1188 bool clipped = false;
1189 gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint(
1190 inverse_screen_space_transform, screen_space_point, &clipped);
1191 gfx::PointF hit_test_point_in_layer_space =
1192 gfx::ScalePoint(hit_test_point_in_content_space,
1193 1.f / layer_content_scale_x,
1194 1.f / layer_content_scale_y);
1195
1196 // If ProjectPoint could not project to a valid value, then we assume that
1197 // this point doesn't hit this region.
1198 if (clipped)
1199 return false;
1200
1201 return layer_space_region.Contains(
1202 gfx::ToRoundedPoint(hit_test_point_in_layer_space));
1203}
1204
[email protected]19aec372014-07-01 19:08:491205static const LayerImpl* GetNextClippingLayer(const LayerImpl* layer) {
[email protected]0ec86f52014-06-12 20:54:031206 if (layer->scroll_parent())
1207 return layer->scroll_parent();
1208 if (layer->clip_parent())
1209 return layer->clip_parent();
1210 return layer->parent();
1211}
1212
[email protected]28336d52014-05-12 19:07:281213static bool PointIsClippedBySurfaceOrClipRect(
1214 const gfx::PointF& screen_space_point,
[email protected]19aec372014-07-01 19:08:491215 const LayerImpl* layer) {
[email protected]28336d52014-05-12 19:07:281216 // Walk up the layer tree and hit-test any render_surfaces and any layer
1217 // clip rects that are active.
[email protected]0ec86f52014-06-12 20:54:031218 for (; layer; layer = GetNextClippingLayer(layer)) {
1219 if (layer->render_surface() &&
1220 !PointHitsRect(screen_space_point,
1221 layer->render_surface()->screen_space_transform(),
1222 layer->render_surface()->content_rect(),
1223 NULL))
[email protected]28336d52014-05-12 19:07:281224 return true;
1225
[email protected]0ec86f52014-06-12 20:54:031226 if (LayerClipsSubtree(layer) &&
1227 !PointHitsRect(screen_space_point,
1228 layer->screen_space_transform(),
1229 gfx::Rect(layer->content_bounds()),
1230 NULL))
[email protected]28336d52014-05-12 19:07:281231 return true;
[email protected]28336d52014-05-12 19:07:281232 }
1233
1234 // If we have finished walking all ancestors without having already exited,
1235 // then the point is not clipped by any ancestors.
1236 return false;
1237}
1238
[email protected]19aec372014-07-01 19:08:491239static bool PointHitsLayer(const LayerImpl* layer,
[email protected]28336d52014-05-12 19:07:281240 const gfx::PointF& screen_space_point,
1241 float* distance_to_intersection) {
1242 gfx::RectF content_rect(layer->content_bounds());
1243 if (!PointHitsRect(screen_space_point,
1244 layer->screen_space_transform(),
1245 content_rect,
1246 distance_to_intersection))
1247 return false;
1248
1249 // At this point, we think the point does hit the layer, but we need to walk
1250 // up the parents to ensure that the layer was not clipped in such a way
1251 // that the hit point actually should not hit the layer.
1252 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer))
1253 return false;
1254
1255 // Skip the HUD layer.
1256 if (layer == layer->layer_tree_impl()->hud_layer())
1257 return false;
1258
1259 return true;
1260}
1261
1262struct FindClosestMatchingLayerDataForRecursion {
1263 FindClosestMatchingLayerDataForRecursion()
1264 : closest_match(NULL),
1265 closest_distance(-std::numeric_limits<float>::infinity()) {}
1266 LayerImpl* closest_match;
1267 // Note that the positive z-axis points towards the camera, so bigger means
1268 // closer in this case, counterintuitively.
1269 float closest_distance;
1270};
1271
1272template <typename Functor>
1273static void FindClosestMatchingLayer(
1274 const gfx::PointF& screen_space_point,
1275 LayerImpl* layer,
1276 const Functor& func,
1277 FindClosestMatchingLayerDataForRecursion* data_for_recursion) {
1278 for (int i = layer->children().size() - 1; i >= 0; --i) {
1279 FindClosestMatchingLayer(
1280 screen_space_point, layer->children()[i], func, data_for_recursion);
1281 }
1282
1283 float distance_to_intersection = 0.f;
1284 if (func(layer) &&
1285 PointHitsLayer(layer, screen_space_point, &distance_to_intersection) &&
1286 ((!data_for_recursion->closest_match ||
1287 distance_to_intersection > data_for_recursion->closest_distance))) {
1288 data_for_recursion->closest_distance = distance_to_intersection;
1289 data_for_recursion->closest_match = layer;
1290 }
1291}
1292
1293static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl* layer) {
1294 if (!layer->scrollable())
1295 return false;
1296 if (layer->IsDrawnRenderSurfaceLayerListMember())
1297 return true;
1298 if (!layer->scroll_children())
1299 return false;
1300 for (std::set<LayerImpl*>::const_iterator it =
1301 layer->scroll_children()->begin();
1302 it != layer->scroll_children()->end();
1303 ++it) {
1304 if ((*it)->IsDrawnRenderSurfaceLayerListMember())
1305 return true;
1306 }
1307 return false;
1308}
1309
1310struct FindScrollingLayerFunctor {
1311 bool operator()(LayerImpl* layer) const {
1312 return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer);
1313 }
1314};
1315
1316LayerImpl* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint(
1317 const gfx::PointF& screen_space_point) {
1318 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1319 FindClosestMatchingLayer(screen_space_point,
1320 root_layer(),
1321 FindScrollingLayerFunctor(),
1322 &data_for_recursion);
1323 return data_for_recursion.closest_match;
1324}
1325
1326struct HitTestVisibleScrollableOrTouchableFunctor {
1327 bool operator()(LayerImpl* layer) const {
1328 return layer->IsDrawnRenderSurfaceLayerListMember() ||
1329 ScrollsAnyDrawnRenderSurfaceLayerListMember(layer) ||
1330 !layer->touch_event_handler_region().IsEmpty() ||
1331 layer->have_wheel_event_handlers();
1332 }
1333};
1334
1335LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPoint(
1336 const gfx::PointF& screen_space_point) {
[email protected]8f7f298822014-06-13 00:23:321337 if (!root_layer())
1338 return NULL;
1339 if (!UpdateDrawProperties())
1340 return NULL;
[email protected]28336d52014-05-12 19:07:281341 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1342 FindClosestMatchingLayer(screen_space_point,
1343 root_layer(),
1344 HitTestVisibleScrollableOrTouchableFunctor(),
1345 &data_for_recursion);
1346 return data_for_recursion.closest_match;
1347}
1348
1349static bool LayerHasTouchEventHandlersAt(const gfx::PointF& screen_space_point,
1350 LayerImpl* layer_impl) {
1351 if (layer_impl->touch_event_handler_region().IsEmpty())
1352 return false;
1353
1354 if (!PointHitsRegion(screen_space_point,
1355 layer_impl->screen_space_transform(),
1356 layer_impl->touch_event_handler_region(),
1357 layer_impl->contents_scale_x(),
1358 layer_impl->contents_scale_y()))
1359 return false;
1360
1361 // At this point, we think the point does hit the touch event handler region
1362 // on the layer, but we need to walk up the parents to ensure that the layer
1363 // was not clipped in such a way that the hit point actually should not hit
1364 // the layer.
1365 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl))
1366 return false;
1367
1368 return true;
1369}
1370
1371struct FindTouchEventLayerFunctor {
1372 bool operator()(LayerImpl* layer) const {
1373 return LayerHasTouchEventHandlersAt(screen_space_point, layer);
1374 }
1375 const gfx::PointF screen_space_point;
1376};
1377
1378LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
1379 const gfx::PointF& screen_space_point) {
[email protected]8f7f298822014-06-13 00:23:321380 if (!root_layer())
1381 return NULL;
1382 if (!UpdateDrawProperties())
1383 return NULL;
[email protected]28336d52014-05-12 19:07:281384 FindTouchEventLayerFunctor func = {screen_space_point};
1385 FindClosestMatchingLayerDataForRecursion data_for_recursion;
1386 FindClosestMatchingLayer(
1387 screen_space_point, root_layer(), func, &data_for_recursion);
1388 return data_for_recursion.closest_match;
1389}
1390
[email protected]ebb179b2014-07-16 17:54:411391void LayerTreeImpl::RegisterSelection(const LayerSelectionBound& start,
1392 const LayerSelectionBound& end) {
1393 selection_start_ = start;
1394 selection_end_ = end;
[email protected]19aec372014-07-01 19:08:491395}
1396
1397static ViewportSelectionBound ComputeViewportSelection(
[email protected]6ef948732014-08-22 18:57:441398 const LayerSelectionBound& layer_bound,
[email protected]19aec372014-07-01 19:08:491399 LayerImpl* layer,
1400 float device_scale_factor) {
[email protected]6ef948732014-08-22 18:57:441401 ViewportSelectionBound viewport_bound;
1402 viewport_bound.type = layer_bound.type;
[email protected]19aec372014-07-01 19:08:491403
[email protected]6ef948732014-08-22 18:57:441404 if (!layer || layer_bound.type == SELECTION_BOUND_EMPTY)
1405 return viewport_bound;
[email protected]19aec372014-07-01 19:08:491406
[email protected]6ef948732014-08-22 18:57:441407 gfx::PointF layer_scaled_top = gfx::ScalePoint(layer_bound.edge_top,
1408 layer->contents_scale_x(),
1409 layer->contents_scale_y());
1410 gfx::PointF layer_scaled_bottom = gfx::ScalePoint(layer_bound.edge_bottom,
1411 layer->contents_scale_x(),
1412 layer->contents_scale_y());
[email protected]19aec372014-07-01 19:08:491413
[email protected]6ef948732014-08-22 18:57:441414 bool clipped = false;
1415 gfx::PointF screen_top = MathUtil::MapPoint(
1416 layer->screen_space_transform(), layer_scaled_top, &clipped);
1417 gfx::PointF screen_bottom = MathUtil::MapPoint(
1418 layer->screen_space_transform(), layer_scaled_bottom, &clipped);
1419
1420 const float inv_scale = 1.f / device_scale_factor;
1421 viewport_bound.edge_top = gfx::ScalePoint(screen_top, inv_scale);
1422 viewport_bound.edge_bottom = gfx::ScalePoint(screen_bottom, inv_scale);
1423
1424 // The bottom edge point is used for visibility testing as it is the logical
[email protected]19aec372014-07-01 19:08:491425 // focal point for bound selection handles (this may change in the future).
[email protected]6ef948732014-08-22 18:57:441426 // Shifting the visibility point fractionally inward ensures that neighboring
1427 // or logically coincident layers aligned to integral DPI coordinates will not
1428 // spuriously occlude the bound.
1429 gfx::Vector2dF visibility_offset = layer_scaled_top - layer_scaled_bottom;
1430 visibility_offset.Scale(device_scale_factor / visibility_offset.Length());
1431 gfx::PointF visibility_point = layer_scaled_bottom + visibility_offset;
1432 if (visibility_point.x() <= 0)
1433 visibility_point.set_x(visibility_point.x() + device_scale_factor);
1434 visibility_point = MathUtil::MapPoint(
1435 layer->screen_space_transform(), visibility_point, &clipped);
1436
[email protected]19aec372014-07-01 19:08:491437 float intersect_distance = 0.f;
[email protected]6ef948732014-08-22 18:57:441438 viewport_bound.visible =
1439 PointHitsLayer(layer, visibility_point, &intersect_distance);
[email protected]19aec372014-07-01 19:08:491440
[email protected]6ef948732014-08-22 18:57:441441 return viewport_bound;
[email protected]19aec372014-07-01 19:08:491442}
1443
[email protected]ebb179b2014-07-16 17:54:411444void LayerTreeImpl::GetViewportSelection(ViewportSelectionBound* start,
1445 ViewportSelectionBound* end) {
1446 DCHECK(start);
1447 DCHECK(end);
[email protected]19aec372014-07-01 19:08:491448
[email protected]ebb179b2014-07-16 17:54:411449 *start = ComputeViewportSelection(
1450 selection_start_,
1451 selection_start_.layer_id ? LayerById(selection_start_.layer_id) : NULL,
[email protected]19aec372014-07-01 19:08:491452 device_scale_factor());
[email protected]ebb179b2014-07-16 17:54:411453 if (start->type == SELECTION_BOUND_CENTER ||
1454 start->type == SELECTION_BOUND_EMPTY) {
1455 *end = *start;
[email protected]19aec372014-07-01 19:08:491456 } else {
[email protected]ebb179b2014-07-16 17:54:411457 *end = ComputeViewportSelection(
1458 selection_end_,
1459 selection_end_.layer_id ? LayerById(selection_end_.layer_id) : NULL,
[email protected]19aec372014-07-01 19:08:491460 device_scale_factor());
1461 }
1462}
1463
[email protected]8aa39ecb2014-06-12 14:19:141464void LayerTreeImpl::RegisterPictureLayerImpl(PictureLayerImpl* layer) {
1465 layer_tree_host_impl_->RegisterPictureLayerImpl(layer);
1466}
1467
1468void LayerTreeImpl::UnregisterPictureLayerImpl(PictureLayerImpl* layer) {
1469 layer_tree_host_impl_->UnregisterPictureLayerImpl(layer);
1470}
1471
[email protected]749cbc62014-07-10 01:06:351472void LayerTreeImpl::InputScrollAnimationFinished() {
1473 layer_tree_host_impl_->ScrollEnd();
1474}
1475
vmpstr56ace232014-10-09 20:16:281476bool LayerTreeImpl::SmoothnessTakesPriority() const {
1477 return layer_tree_host_impl_->GetTreePriority() == SMOOTHNESS_TAKES_PRIORITY;
1478}
1479
skyostil3976a3f2014-09-04 22:07:231480BlockingTaskRunner* LayerTreeImpl::BlockingMainThreadTaskRunner() const {
1481 return proxy()->blocking_main_thread_task_runner();
1482}
1483
bokan915bf352014-10-02 21:57:141484void LayerTreeImpl::SetPageScaleAnimation(
1485 const gfx::Vector2d& target_offset,
1486 bool anchor_point,
1487 float page_scale,
1488 base::TimeDelta duration) {
1489 if (!InnerViewportScrollLayer())
1490 return;
1491
1492 gfx::ScrollOffset scroll_total = TotalScrollOffset();
1493 gfx::SizeF scaled_scrollable_size = ScrollableSize();
1494 gfx::SizeF viewport_size = InnerViewportContainerLayer()->bounds();
1495
1496 // Easing constants experimentally determined.
1497 scoped_ptr<TimingFunction> timing_function =
1498 CubicBezierTimingFunction::Create(.8, 0, .3, .9);
1499
1500 // TODO(miletus) : Pass in ScrollOffset.
1501 page_scale_animation_ =
1502 PageScaleAnimation::Create(ScrollOffsetToVector2dF(scroll_total),
1503 total_page_scale_factor(),
1504 viewport_size,
1505 scaled_scrollable_size,
1506 timing_function.Pass());
1507
1508 if (anchor_point) {
1509 gfx::Vector2dF anchor(target_offset);
1510 page_scale_animation_->ZoomWithAnchor(anchor,
1511 page_scale,
1512 duration.InSecondsF());
1513 } else {
1514 gfx::Vector2dF scaled_target_offset = target_offset;
1515 page_scale_animation_->ZoomTo(scaled_target_offset,
1516 page_scale,
1517 duration.InSecondsF());
1518 }
1519}
1520
1521scoped_ptr<PageScaleAnimation> LayerTreeImpl::TakePageScaleAnimation() {
1522 return page_scale_animation_.Pass();
1523}
1524
[email protected]ca2902e92013-03-28 01:45:351525} // namespace cc