[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/layer_tree_impl.h" |
| 6 | |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 7 | #include "base/debug/trace_event.h" |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 8 | #include "cc/layer_tree_host_common.h" |
| 9 | #include "cc/layer_tree_host_impl.h" |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 10 | #include "ui/gfx/vector2d_conversions.h" |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 11 | |
| 12 | namespace cc { |
| 13 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 14 | LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl) |
| 15 | : layer_tree_host_impl_(layer_tree_host_impl) |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 16 | , source_frame_number_(-1) |
| 17 | , hud_layer_(0) |
| 18 | , root_scroll_layer_(0) |
| 19 | , currently_scrolling_layer_(0) |
[email protected] | a3029014 | 2013-01-05 01:27:00 | [diff] [blame] | 20 | , background_color_(0) |
| 21 | , has_transparent_background_(false) |
[email protected] | 6f90b9e | 2013-01-17 23:42:00 | [diff] [blame] | 22 | , scrolling_layer_id_from_previous_tree_(0) |
| 23 | , contents_textures_purged_(false) { |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | LayerTreeImpl::~LayerTreeImpl() { |
[email protected] | 361bc00d | 2012-12-14 07:03:24 | [diff] [blame] | 27 | // Need to explicitly clear the tree prior to destroying this so that |
| 28 | // the LayerTreeImpl pointer is still valid in the LayerImpl dtor. |
| 29 | root_layer_.reset(); |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static LayerImpl* findRootScrollLayer(LayerImpl* layer) |
| 33 | { |
| 34 | if (!layer) |
| 35 | return 0; |
| 36 | |
| 37 | if (layer->scrollable()) |
| 38 | return layer; |
| 39 | |
| 40 | for (size_t i = 0; i < layer->children().size(); ++i) { |
| 41 | LayerImpl* found = findRootScrollLayer(layer->children()[i]); |
| 42 | if (found) |
| 43 | return found; |
| 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) { |
| 50 | root_layer_ = layer.Pass(); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 51 | root_scroll_layer_ = NULL; |
| 52 | currently_scrolling_layer_ = NULL; |
| 53 | |
| 54 | layer_tree_host_impl_->OnCanDrawStateChangedForTree(this); |
| 55 | } |
| 56 | |
| 57 | void LayerTreeImpl::FindRootScrollLayer() { |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 58 | root_scroll_layer_ = findRootScrollLayer(root_layer_.get()); |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 59 | |
| 60 | if (root_layer_ && scrolling_layer_id_from_previous_tree_) { |
| 61 | currently_scrolling_layer_ = LayerTreeHostCommon::findLayerInSubtree( |
| 62 | root_layer_.get(), |
| 63 | scrolling_layer_id_from_previous_tree_); |
| 64 | } |
| 65 | |
| 66 | scrolling_layer_id_from_previous_tree_ = 0; |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() { |
| 70 | // Clear all data structures that have direct references to the layer tree. |
| 71 | scrolling_layer_id_from_previous_tree_ = |
| 72 | currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0; |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 73 | root_scroll_layer_ = NULL; |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 74 | currently_scrolling_layer_ = NULL; |
| 75 | |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 76 | render_surface_layer_list_.clear(); |
| 77 | SetNeedsUpdateDrawProperties(); |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 78 | return root_layer_.Pass(); |
| 79 | } |
| 80 | |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 81 | LayerImpl* LayerTreeImpl::RootScrollLayer() { |
| 82 | DCHECK(IsActiveTree()); |
| 83 | return root_scroll_layer_; |
| 84 | } |
| 85 | |
| 86 | LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() { |
| 87 | DCHECK(IsActiveTree()); |
| 88 | return currently_scrolling_layer_; |
| 89 | } |
| 90 | |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 91 | void LayerTreeImpl::ClearCurrentlyScrollingLayer() { |
| 92 | currently_scrolling_layer_ = NULL; |
| 93 | scrolling_layer_id_from_previous_tree_ = 0; |
| 94 | } |
| 95 | |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 96 | void LayerTreeImpl::UpdateMaxScrollOffset() { |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 97 | if (!root_scroll_layer_ || !root_scroll_layer_->children().size()) |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 98 | return; |
| 99 | |
[email protected] | 42ccdbef | 2013-01-21 07:54:54 | [diff] [blame^] | 100 | gfx::SizeF view_bounds; |
| 101 | if (!settings().pageScalePinchZoomEnabled) { |
| 102 | view_bounds = device_viewport_size(); |
| 103 | if (LayerImpl* clip_layer = root_scroll_layer_->parent()) { |
| 104 | // Compensate for non-overlay scrollbars. |
| 105 | if (clip_layer->masksToBounds()) |
| 106 | view_bounds = gfx::ScaleSize(clip_layer->bounds(), device_scale_factor()); |
| 107 | } |
[email protected] | a823df8 | 2013-01-10 02:38:17 | [diff] [blame] | 108 | view_bounds.Scale(1 / pinch_zoom_viewport().page_scale_delta()); |
[email protected] | 42ccdbef | 2013-01-21 07:54:54 | [diff] [blame^] | 109 | } else { |
| 110 | view_bounds = layout_viewport_size(); |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 42ccdbef | 2013-01-21 07:54:54 | [diff] [blame^] | 113 | gfx::Vector2dF max_scroll = gfx::Rect(ScrollableSize()).bottom_right() - |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 114 | gfx::RectF(view_bounds).bottom_right(); |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 115 | |
| 116 | // The viewport may be larger than the contents in some cases, such as |
| 117 | // having a vertical scrollbar but no horizontal overflow. |
| 118 | max_scroll.ClampToMin(gfx::Vector2dF()); |
| 119 | |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 120 | root_scroll_layer_->setMaxScrollOffset(gfx::ToFlooredVector2d(max_scroll)); |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 123 | void LayerTreeImpl::UpdateDrawProperties() { |
| 124 | render_surface_layer_list_.clear(); |
| 125 | if (!RootLayer()) |
| 126 | return; |
| 127 | |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 128 | if (root_scroll_layer_) { |
| 129 | root_scroll_layer_->setImplTransform( |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 130 | layer_tree_host_impl_->implTransform()); |
| 131 | } |
| 132 | |
| 133 | { |
| 134 | TRACE_EVENT0("cc", "LayerTreeImpl::UpdateDrawProperties"); |
| 135 | LayerTreeHostCommon::calculateDrawProperties( |
| 136 | RootLayer(), |
| 137 | device_viewport_size(), |
| 138 | device_scale_factor(), |
[email protected] | a823df8 | 2013-01-10 02:38:17 | [diff] [blame] | 139 | pinch_zoom_viewport().page_scale_factor(), |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 140 | MaxTextureSize(), |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 141 | settings().canUseLCDText, |
| 142 | render_surface_layer_list_); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void ClearRenderSurfacesOnLayerImplRecursive(LayerImpl* current) |
| 147 | { |
| 148 | DCHECK(current); |
| 149 | for (size_t i = 0; i < current->children().size(); ++i) |
| 150 | ClearRenderSurfacesOnLayerImplRecursive(current->children()[i]); |
| 151 | current->clearRenderSurface(); |
| 152 | } |
| 153 | |
| 154 | void LayerTreeImpl::ClearRenderSurfaces() { |
| 155 | ClearRenderSurfacesOnLayerImplRecursive(RootLayer()); |
| 156 | render_surface_layer_list_.clear(); |
| 157 | SetNeedsUpdateDrawProperties(); |
| 158 | } |
| 159 | |
[email protected] | b0a917c8d | 2013-01-12 17:42:25 | [diff] [blame] | 160 | bool LayerTreeImpl::AreVisibleResourcesReady() const { |
| 161 | TRACE_EVENT0("cc", "LayerTreeImpl::AreVisibleResourcesReady"); |
| 162 | |
| 163 | typedef LayerIterator<LayerImpl, |
| 164 | std::vector<LayerImpl*>, |
| 165 | RenderSurfaceImpl, |
| 166 | LayerIteratorActions::BackToFront> LayerIteratorType; |
| 167 | LayerIteratorType end = LayerIteratorType::end(&render_surface_layer_list_); |
| 168 | for (LayerIteratorType it = LayerIteratorType::begin( |
| 169 | &render_surface_layer_list_); it != end; ++it) { |
| 170 | if (it.representsItself() && !(*it)->areVisibleResourcesReady()) |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
[email protected] | 76ffd9e | 2012-12-20 19:12:47 | [diff] [blame] | 177 | const LayerTreeImpl::LayerList& LayerTreeImpl::RenderSurfaceLayerList() const { |
| 178 | // If this assert triggers, then the list is dirty. |
| 179 | DCHECK(!layer_tree_host_impl_->needsUpdateDrawProperties()); |
| 180 | return render_surface_layer_list_; |
| 181 | } |
| 182 | |
[email protected] | 42ccdbef | 2013-01-21 07:54:54 | [diff] [blame^] | 183 | gfx::Size LayerTreeImpl::ScrollableSize() const { |
[email protected] | c4d467a | 2013-01-21 03:21:01 | [diff] [blame] | 184 | if (!root_scroll_layer_ || root_scroll_layer_->children().empty()) |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 185 | return gfx::Size(); |
[email protected] | c4d467a | 2013-01-21 03:21:01 | [diff] [blame] | 186 | return root_scroll_layer_->children()[0]->bounds(); |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 361bc00d | 2012-12-14 07:03:24 | [diff] [blame] | 189 | LayerImpl* LayerTreeImpl::LayerById(int id) { |
| 190 | LayerIdMap::iterator iter = layer_id_map_.find(id); |
| 191 | return iter != layer_id_map_.end() ? iter->second : NULL; |
| 192 | } |
| 193 | |
| 194 | void LayerTreeImpl::RegisterLayer(LayerImpl* layer) { |
| 195 | DCHECK(!LayerById(layer->id())); |
| 196 | layer_id_map_[layer->id()] = layer; |
| 197 | } |
| 198 | |
| 199 | void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) { |
| 200 | DCHECK(LayerById(layer->id())); |
| 201 | layer_id_map_.erase(layer->id()); |
| 202 | } |
| 203 | |
[email protected] | 1e0f8d6 | 2013-01-09 07:41:35 | [diff] [blame] | 204 | void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pendingTree) { |
| 205 | int id = currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0; |
| 206 | pendingTree->set_currently_scrolling_layer( |
| 207 | LayerTreeHostCommon::findLayerInSubtree(pendingTree->RootLayer(), id)); |
| 208 | } |
| 209 | |
[email protected] | 37386f05 | 2013-01-13 00:42:22 | [diff] [blame] | 210 | static void DidBecomeActiveRecursive(LayerImpl* layer) { |
| 211 | layer->didBecomeActive(); |
| 212 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 213 | DidBecomeActiveRecursive(layer->children()[i]); |
| 214 | } |
| 215 | |
| 216 | void LayerTreeImpl::DidBecomeActive() { |
| 217 | if (RootLayer()) |
| 218 | DidBecomeActiveRecursive(RootLayer()); |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 219 | FindRootScrollLayer(); |
| 220 | UpdateMaxScrollOffset(); |
[email protected] | 37386f05 | 2013-01-13 00:42:22 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 6f90b9e | 2013-01-17 23:42:00 | [diff] [blame] | 223 | bool LayerTreeImpl::ContentsTexturesPurged() const { |
| 224 | return contents_textures_purged_; |
| 225 | } |
| 226 | |
| 227 | void LayerTreeImpl::SetContentsTexturesPurged() { |
| 228 | contents_textures_purged_ = true; |
| 229 | layer_tree_host_impl_->OnCanDrawStateChangedForTree(this); |
| 230 | } |
| 231 | |
| 232 | void LayerTreeImpl::ResetContentsTexturesPurged() { |
| 233 | contents_textures_purged_ = false; |
| 234 | layer_tree_host_impl_->OnCanDrawStateChangedForTree(this); |
| 235 | } |
| 236 | |
[email protected] | ff762fb | 2012-12-12 19:18:37 | [diff] [blame] | 237 | const LayerTreeSettings& LayerTreeImpl::settings() const { |
| 238 | return layer_tree_host_impl_->settings(); |
| 239 | } |
| 240 | |
| 241 | OutputSurface* LayerTreeImpl::output_surface() const { |
| 242 | return layer_tree_host_impl_->outputSurface(); |
| 243 | } |
| 244 | |
| 245 | ResourceProvider* LayerTreeImpl::resource_provider() const { |
| 246 | return layer_tree_host_impl_->resourceProvider(); |
| 247 | } |
| 248 | |
| 249 | TileManager* LayerTreeImpl::tile_manager() const { |
| 250 | return layer_tree_host_impl_->tileManager(); |
| 251 | } |
| 252 | |
| 253 | FrameRateCounter* LayerTreeImpl::frame_rate_counter() const { |
| 254 | return layer_tree_host_impl_->fpsCounter(); |
| 255 | } |
| 256 | |
[email protected] | 71691c2 | 2013-01-18 03:14:22 | [diff] [blame] | 257 | PaintTimeCounter* LayerTreeImpl::paint_time_counter() const { |
| 258 | return layer_tree_host_impl_->paintTimeCounter(); |
| 259 | } |
| 260 | |
[email protected] | f117a4c | 2012-12-16 04:53:10 | [diff] [blame] | 261 | bool LayerTreeImpl::IsActiveTree() const { |
| 262 | return layer_tree_host_impl_->activeTree() == this; |
| 263 | } |
| 264 | |
| 265 | bool LayerTreeImpl::IsPendingTree() const { |
| 266 | return layer_tree_host_impl_->pendingTree() == this; |
| 267 | } |
| 268 | |
| 269 | LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) { |
| 270 | LayerTreeImpl* tree = layer_tree_host_impl_->activeTree(); |
| 271 | if (!tree) |
| 272 | return NULL; |
| 273 | return tree->LayerById(id); |
| 274 | } |
| 275 | |
| 276 | LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) { |
| 277 | LayerTreeImpl* tree = layer_tree_host_impl_->pendingTree(); |
| 278 | if (!tree) |
| 279 | return NULL; |
| 280 | return tree->LayerById(id); |
| 281 | } |
| 282 | |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 283 | int LayerTreeImpl::MaxTextureSize() const { |
| 284 | return layer_tree_host_impl_->rendererCapabilities().maxTextureSize; |
| 285 | } |
| 286 | |
[email protected] | 166db5c8 | 2013-01-09 23:54:31 | [diff] [blame] | 287 | bool LayerTreeImpl::PinchGestureActive() const { |
| 288 | return layer_tree_host_impl_->pinchGestureActive(); |
| 289 | } |
| 290 | |
[email protected] | ff762fb | 2012-12-12 19:18:37 | [diff] [blame] | 291 | void LayerTreeImpl::SetNeedsRedraw() { |
| 292 | layer_tree_host_impl_->setNeedsRedraw(); |
| 293 | } |
| 294 | |
| 295 | void LayerTreeImpl::SetNeedsUpdateDrawProperties() { |
| 296 | layer_tree_host_impl_->setNeedsUpdateDrawProperties(); |
| 297 | } |
| 298 | |
| 299 | const LayerTreeDebugState& LayerTreeImpl::debug_state() const { |
| 300 | return layer_tree_host_impl_->debugState(); |
| 301 | } |
| 302 | |
| 303 | float LayerTreeImpl::device_scale_factor() const { |
| 304 | return layer_tree_host_impl_->deviceScaleFactor(); |
| 305 | } |
| 306 | |
| 307 | const gfx::Size& LayerTreeImpl::device_viewport_size() const { |
| 308 | return layer_tree_host_impl_->deviceViewportSize(); |
| 309 | } |
| 310 | |
| 311 | const gfx::Size& LayerTreeImpl::layout_viewport_size() const { |
| 312 | return layer_tree_host_impl_->layoutViewportSize(); |
| 313 | } |
| 314 | |
| 315 | std::string LayerTreeImpl::layer_tree_as_text() const { |
| 316 | return layer_tree_host_impl_->layerTreeAsText(); |
| 317 | } |
| 318 | |
| 319 | DebugRectHistory* LayerTreeImpl::debug_rect_history() const { |
| 320 | return layer_tree_host_impl_->debugRectHistory(); |
| 321 | } |
| 322 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame] | 323 | AnimationRegistrar* LayerTreeImpl::animationRegistrar() const { |
| 324 | return layer_tree_host_impl_->animationRegistrar(); |
| 325 | } |
[email protected] | ff762fb | 2012-12-12 19:18:37 | [diff] [blame] | 326 | |
[email protected] | caa567d | 2012-12-20 07:56:16 | [diff] [blame] | 327 | const PinchZoomViewport& LayerTreeImpl::pinch_zoom_viewport() const { |
| 328 | return layer_tree_host_impl_->pinchZoomViewport(); |
| 329 | } |
| 330 | |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 331 | } // namespace cc |