blob: f7e59b44a563ed2dbadbe4d387bf8e2ea5aad33e [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
5#include "cc/layer_tree_impl.h"
6
[email protected]76ffd9e2012-12-20 19:12:477#include "base/debug/trace_event.h"
[email protected]3b31c6ac2012-12-06 21:27:298#include "cc/layer_tree_host_common.h"
9#include "cc/layer_tree_host_impl.h"
[email protected]caa567d2012-12-20 07:56:1610#include "ui/gfx/vector2d_conversions.h"
[email protected]3b31c6ac2012-12-06 21:27:2911
12namespace cc {
13
[email protected]8bef40572012-12-11 21:38:0814LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl)
15 : layer_tree_host_impl_(layer_tree_host_impl)
[email protected]3b31c6ac2012-12-06 21:27:2916 , source_frame_number_(-1)
17 , hud_layer_(0)
18 , root_scroll_layer_(0)
19 , currently_scrolling_layer_(0)
[email protected]a30290142013-01-05 01:27:0020 , background_color_(0)
21 , has_transparent_background_(false)
[email protected]6f90b9e2013-01-17 23:42:0022 , scrolling_layer_id_from_previous_tree_(0)
[email protected]615c78a2013-01-24 23:44:1623 , contents_textures_purged_(false)
24 , needs_update_draw_properties_(true) {
[email protected]3b31c6ac2012-12-06 21:27:2925}
26
27LayerTreeImpl::~LayerTreeImpl() {
[email protected]361bc00d2012-12-14 07:03:2428 // Need to explicitly clear the tree prior to destroying this so that
29 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
30 root_layer_.reset();
[email protected]3b31c6ac2012-12-06 21:27:2931}
32
33static LayerImpl* findRootScrollLayer(LayerImpl* layer)
34{
35 if (!layer)
36 return 0;
37
38 if (layer->scrollable())
39 return layer;
40
41 for (size_t i = 0; i < layer->children().size(); ++i) {
42 LayerImpl* found = findRootScrollLayer(layer->children()[i]);
43 if (found)
44 return found;
45 }
46
47 return 0;
48}
49
50void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
51 root_layer_ = layer.Pass();
[email protected]5c4824e12013-01-12 16:34:5352 root_scroll_layer_ = NULL;
53 currently_scrolling_layer_ = NULL;
54
55 layer_tree_host_impl_->OnCanDrawStateChangedForTree(this);
56}
57
58void LayerTreeImpl::FindRootScrollLayer() {
[email protected]3b31c6ac2012-12-06 21:27:2959 root_scroll_layer_ = findRootScrollLayer(root_layer_.get());
[email protected]3b31c6ac2012-12-06 21:27:2960
61 if (root_layer_ && scrolling_layer_id_from_previous_tree_) {
62 currently_scrolling_layer_ = LayerTreeHostCommon::findLayerInSubtree(
63 root_layer_.get(),
64 scrolling_layer_id_from_previous_tree_);
65 }
66
67 scrolling_layer_id_from_previous_tree_ = 0;
[email protected]3b31c6ac2012-12-06 21:27:2968}
69
70scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
71 // Clear all data structures that have direct references to the layer tree.
72 scrolling_layer_id_from_previous_tree_ =
73 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
[email protected]69b50ec2013-01-19 04:58:0174 root_scroll_layer_ = NULL;
[email protected]3b31c6ac2012-12-06 21:27:2975 currently_scrolling_layer_ = NULL;
76
[email protected]76ffd9e2012-12-20 19:12:4777 render_surface_layer_list_.clear();
[email protected]615c78a2013-01-24 23:44:1678 set_needs_update_draw_properties();
[email protected]3b31c6ac2012-12-06 21:27:2979 return root_layer_.Pass();
80}
81
[email protected]69b50ec2013-01-19 04:58:0182LayerImpl* LayerTreeImpl::RootScrollLayer() {
83 DCHECK(IsActiveTree());
84 return root_scroll_layer_;
85}
86
87LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() {
88 DCHECK(IsActiveTree());
89 return currently_scrolling_layer_;
90}
91
[email protected]3b31c6ac2012-12-06 21:27:2992void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
93 currently_scrolling_layer_ = NULL;
94 scrolling_layer_id_from_previous_tree_ = 0;
95}
96
[email protected]caa567d2012-12-20 07:56:1697void LayerTreeImpl::UpdateMaxScrollOffset() {
[email protected]69b50ec2013-01-19 04:58:0198 if (!root_scroll_layer_ || !root_scroll_layer_->children().size())
[email protected]caa567d2012-12-20 07:56:1699 return;
100
[email protected]42ccdbef2013-01-21 07:54:54101 gfx::SizeF view_bounds;
102 if (!settings().pageScalePinchZoomEnabled) {
103 view_bounds = device_viewport_size();
104 if (LayerImpl* clip_layer = root_scroll_layer_->parent()) {
105 // Compensate for non-overlay scrollbars.
106 if (clip_layer->masksToBounds())
107 view_bounds = gfx::ScaleSize(clip_layer->bounds(), device_scale_factor());
108 }
[email protected]a823df82013-01-10 02:38:17109 view_bounds.Scale(1 / pinch_zoom_viewport().page_scale_delta());
[email protected]42ccdbef2013-01-21 07:54:54110 } else {
111 view_bounds = layout_viewport_size();
[email protected]caa567d2012-12-20 07:56:16112 }
113
[email protected]42ccdbef2013-01-21 07:54:54114 gfx::Vector2dF max_scroll = gfx::Rect(ScrollableSize()).bottom_right() -
[email protected]caa567d2012-12-20 07:56:16115 gfx::RectF(view_bounds).bottom_right();
[email protected]caa567d2012-12-20 07:56:16116
117 // The viewport may be larger than the contents in some cases, such as
118 // having a vertical scrollbar but no horizontal overflow.
119 max_scroll.ClampToMin(gfx::Vector2dF());
120
[email protected]69b50ec2013-01-19 04:58:01121 root_scroll_layer_->setMaxScrollOffset(gfx::ToFlooredVector2d(max_scroll));
[email protected]caa567d2012-12-20 07:56:16122}
123
[email protected]4c9bb952013-01-27 05:41:18124struct UpdateTilePrioritiesForLayer {
125 void operator()(LayerImpl *layer) {
126 layer->updateTilePriorities();
127 }
128};
129
130void LayerTreeImpl::UpdateDrawProperties(UpdateDrawPropertiesReason reason) {
131 if (!needs_update_draw_properties_) {
132 if (reason == UPDATE_ACTIVE_TREE_FOR_DRAW && RootLayer())
133 LayerTreeHostCommon::callFunctionForSubtree<UpdateTilePrioritiesForLayer>(
134 RootLayer());
[email protected]615c78a2013-01-24 23:44:16135 return;
[email protected]4c9bb952013-01-27 05:41:18136 }
[email protected]615c78a2013-01-24 23:44:16137
138 needs_update_draw_properties_ = false;
[email protected]76ffd9e2012-12-20 19:12:47139 render_surface_layer_list_.clear();
[email protected]615c78a2013-01-24 23:44:16140
141 // For maxTextureSize.
142 if (!layer_tree_host_impl_->renderer())
143 return;
144
[email protected]76ffd9e2012-12-20 19:12:47145 if (!RootLayer())
146 return;
147
[email protected]69b50ec2013-01-19 04:58:01148 if (root_scroll_layer_) {
149 root_scroll_layer_->setImplTransform(
[email protected]76ffd9e2012-12-20 19:12:47150 layer_tree_host_impl_->implTransform());
[email protected]615c78a2013-01-24 23:44:16151 // Setting the impl transform re-sets this.
152 needs_update_draw_properties_ = false;
[email protected]76ffd9e2012-12-20 19:12:47153 }
154
155 {
[email protected]615c78a2013-01-24 23:44:16156 TRACE_EVENT1("cc", "LayerTreeImpl::UpdateDrawProperties", "IsActive", IsActiveTree());
[email protected]4c9bb952013-01-27 05:41:18157 bool update_tile_priorities =
158 reason == UPDATE_PENDING_TREE ||
159 reason == UPDATE_ACTIVE_TREE_FOR_DRAW;
[email protected]76ffd9e2012-12-20 19:12:47160 LayerTreeHostCommon::calculateDrawProperties(
161 RootLayer(),
162 device_viewport_size(),
163 device_scale_factor(),
[email protected]d8fd0532013-01-26 02:09:49164 pinch_zoom_viewport().total_page_scale_factor(),
[email protected]f6776532012-12-21 20:24:33165 MaxTextureSize(),
[email protected]76ffd9e2012-12-20 19:12:47166 settings().canUseLCDText,
[email protected]4c9bb952013-01-27 05:41:18167 render_surface_layer_list_,
168 update_tile_priorities);
[email protected]76ffd9e2012-12-20 19:12:47169 }
[email protected]615c78a2013-01-24 23:44:16170
171 DCHECK(!needs_update_draw_properties_) <<
172 "calcDrawProperties should not set_needs_update_draw_properties()";
[email protected]76ffd9e2012-12-20 19:12:47173}
174
175static void ClearRenderSurfacesOnLayerImplRecursive(LayerImpl* current)
176{
177 DCHECK(current);
178 for (size_t i = 0; i < current->children().size(); ++i)
179 ClearRenderSurfacesOnLayerImplRecursive(current->children()[i]);
180 current->clearRenderSurface();
181}
182
183void LayerTreeImpl::ClearRenderSurfaces() {
184 ClearRenderSurfacesOnLayerImplRecursive(RootLayer());
185 render_surface_layer_list_.clear();
[email protected]615c78a2013-01-24 23:44:16186 set_needs_update_draw_properties();
[email protected]76ffd9e2012-12-20 19:12:47187}
188
[email protected]b0a917c8d2013-01-12 17:42:25189bool LayerTreeImpl::AreVisibleResourcesReady() const {
190 TRACE_EVENT0("cc", "LayerTreeImpl::AreVisibleResourcesReady");
191
192 typedef LayerIterator<LayerImpl,
193 std::vector<LayerImpl*>,
194 RenderSurfaceImpl,
195 LayerIteratorActions::BackToFront> LayerIteratorType;
196 LayerIteratorType end = LayerIteratorType::end(&render_surface_layer_list_);
197 for (LayerIteratorType it = LayerIteratorType::begin(
198 &render_surface_layer_list_); it != end; ++it) {
199 if (it.representsItself() && !(*it)->areVisibleResourcesReady())
200 return false;
201 }
202
203 return true;
204}
205
[email protected]76ffd9e2012-12-20 19:12:47206const LayerTreeImpl::LayerList& LayerTreeImpl::RenderSurfaceLayerList() const {
207 // If this assert triggers, then the list is dirty.
[email protected]615c78a2013-01-24 23:44:16208 DCHECK(!needs_update_draw_properties_);
[email protected]76ffd9e2012-12-20 19:12:47209 return render_surface_layer_list_;
210}
211
[email protected]42ccdbef2013-01-21 07:54:54212gfx::Size LayerTreeImpl::ScrollableSize() const {
[email protected]c4d467a2013-01-21 03:21:01213 if (!root_scroll_layer_ || root_scroll_layer_->children().empty())
[email protected]caa567d2012-12-20 07:56:16214 return gfx::Size();
[email protected]c4d467a2013-01-21 03:21:01215 return root_scroll_layer_->children()[0]->bounds();
[email protected]caa567d2012-12-20 07:56:16216}
217
[email protected]361bc00d2012-12-14 07:03:24218LayerImpl* LayerTreeImpl::LayerById(int id) {
219 LayerIdMap::iterator iter = layer_id_map_.find(id);
220 return iter != layer_id_map_.end() ? iter->second : NULL;
221}
222
223void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
224 DCHECK(!LayerById(layer->id()));
225 layer_id_map_[layer->id()] = layer;
226}
227
228void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
229 DCHECK(LayerById(layer->id()));
230 layer_id_map_.erase(layer->id());
231}
232
[email protected]1e0f8d62013-01-09 07:41:35233void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pendingTree) {
234 int id = currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
235 pendingTree->set_currently_scrolling_layer(
236 LayerTreeHostCommon::findLayerInSubtree(pendingTree->RootLayer(), id));
237}
238
[email protected]37386f052013-01-13 00:42:22239static void DidBecomeActiveRecursive(LayerImpl* layer) {
240 layer->didBecomeActive();
241 for (size_t i = 0; i < layer->children().size(); ++i)
242 DidBecomeActiveRecursive(layer->children()[i]);
243}
244
245void LayerTreeImpl::DidBecomeActive() {
246 if (RootLayer())
247 DidBecomeActiveRecursive(RootLayer());
[email protected]69b50ec2013-01-19 04:58:01248 FindRootScrollLayer();
249 UpdateMaxScrollOffset();
[email protected]37386f052013-01-13 00:42:22250}
251
[email protected]6f90b9e2013-01-17 23:42:00252bool LayerTreeImpl::ContentsTexturesPurged() const {
253 return contents_textures_purged_;
254}
255
256void LayerTreeImpl::SetContentsTexturesPurged() {
257 contents_textures_purged_ = true;
258 layer_tree_host_impl_->OnCanDrawStateChangedForTree(this);
259}
260
261void LayerTreeImpl::ResetContentsTexturesPurged() {
262 contents_textures_purged_ = false;
263 layer_tree_host_impl_->OnCanDrawStateChangedForTree(this);
264}
265
[email protected]48871fc2013-01-23 07:36:51266Proxy* LayerTreeImpl::proxy() const {
267 return layer_tree_host_impl_->proxy();
268}
269
[email protected]ff762fb2012-12-12 19:18:37270const LayerTreeSettings& LayerTreeImpl::settings() const {
271 return layer_tree_host_impl_->settings();
272}
273
274OutputSurface* LayerTreeImpl::output_surface() const {
275 return layer_tree_host_impl_->outputSurface();
276}
277
278ResourceProvider* LayerTreeImpl::resource_provider() const {
279 return layer_tree_host_impl_->resourceProvider();
280}
281
282TileManager* LayerTreeImpl::tile_manager() const {
283 return layer_tree_host_impl_->tileManager();
284}
285
286FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
287 return layer_tree_host_impl_->fpsCounter();
288}
289
[email protected]71691c22013-01-18 03:14:22290PaintTimeCounter* LayerTreeImpl::paint_time_counter() const {
291 return layer_tree_host_impl_->paintTimeCounter();
292}
293
[email protected]f117a4c2012-12-16 04:53:10294bool LayerTreeImpl::IsActiveTree() const {
295 return layer_tree_host_impl_->activeTree() == this;
296}
297
298bool LayerTreeImpl::IsPendingTree() const {
299 return layer_tree_host_impl_->pendingTree() == this;
300}
301
[email protected]48871fc2013-01-23 07:36:51302bool LayerTreeImpl::IsRecycleTree() const {
303 return layer_tree_host_impl_->recycleTree() == this;
304}
305
[email protected]f117a4c2012-12-16 04:53:10306LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
307 LayerTreeImpl* tree = layer_tree_host_impl_->activeTree();
308 if (!tree)
309 return NULL;
310 return tree->LayerById(id);
311}
312
313LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
314 LayerTreeImpl* tree = layer_tree_host_impl_->pendingTree();
315 if (!tree)
316 return NULL;
317 return tree->LayerById(id);
318}
319
[email protected]f6776532012-12-21 20:24:33320int LayerTreeImpl::MaxTextureSize() const {
321 return layer_tree_host_impl_->rendererCapabilities().maxTextureSize;
322}
323
[email protected]166db5c82013-01-09 23:54:31324bool LayerTreeImpl::PinchGestureActive() const {
325 return layer_tree_host_impl_->pinchGestureActive();
326}
327
[email protected]ff762fb2012-12-12 19:18:37328void LayerTreeImpl::SetNeedsRedraw() {
329 layer_tree_host_impl_->setNeedsRedraw();
330}
331
[email protected]ff762fb2012-12-12 19:18:37332const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
333 return layer_tree_host_impl_->debugState();
334}
335
336float LayerTreeImpl::device_scale_factor() const {
337 return layer_tree_host_impl_->deviceScaleFactor();
338}
339
340const gfx::Size& LayerTreeImpl::device_viewport_size() const {
341 return layer_tree_host_impl_->deviceViewportSize();
342}
343
344const gfx::Size& LayerTreeImpl::layout_viewport_size() const {
345 return layer_tree_host_impl_->layoutViewportSize();
346}
347
348std::string LayerTreeImpl::layer_tree_as_text() const {
349 return layer_tree_host_impl_->layerTreeAsText();
350}
351
352DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
353 return layer_tree_host_impl_->debugRectHistory();
354}
355
[email protected]de4afb5e2012-12-20 00:11:34356AnimationRegistrar* LayerTreeImpl::animationRegistrar() const {
357 return layer_tree_host_impl_->animationRegistrar();
358}
[email protected]ff762fb2012-12-12 19:18:37359
[email protected]caa567d2012-12-20 07:56:16360const PinchZoomViewport& LayerTreeImpl::pinch_zoom_viewport() const {
361 return layer_tree_host_impl_->pinchZoomViewport();
362}
363
[email protected]3b31c6ac2012-12-06 21:27:29364} // namespace cc