Rename ScrollbarLayer\* to PaintedScrollbarLayer\*
[chromium-blink-merge.git] / cc / trees / layer_tree_impl.cc
blobf9b294b5d35f96c2898f919305a3744f78b854ec
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/layer_tree_impl.h"
7 #include "base/debug/trace_event.h"
8 #include "cc/animation/keyframed_animation_curve.h"
9 #include "cc/animation/scrollbar_animation_controller.h"
10 #include "cc/debug/traced_value.h"
11 #include "cc/layers/heads_up_display_layer_impl.h"
12 #include "cc/layers/painted_scrollbar_layer_impl.h"
13 #include "cc/layers/render_surface_impl.h"
14 #include "cc/trees/layer_tree_host_common.h"
15 #include "cc/trees/layer_tree_host_impl.h"
16 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/vector2d_conversions.h"
19 namespace cc {
21 LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl)
22 : layer_tree_host_impl_(layer_tree_host_impl),
23 source_frame_number_(-1),
24 hud_layer_(0),
25 root_scroll_layer_(NULL),
26 currently_scrolling_layer_(NULL),
27 root_layer_scroll_offset_delegate_(NULL),
28 background_color_(0),
29 has_transparent_background_(false),
30 page_scale_factor_(1),
31 page_scale_delta_(1),
32 sent_page_scale_delta_(1),
33 min_page_scale_factor_(0),
34 max_page_scale_factor_(0),
35 scrolling_layer_id_from_previous_tree_(0),
36 contents_textures_purged_(false),
37 viewport_size_invalid_(false),
38 needs_update_draw_properties_(true),
39 needs_full_tree_sync_(true) {
42 LayerTreeImpl::~LayerTreeImpl() {
43 // Need to explicitly clear the tree prior to destroying this so that
44 // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
45 root_layer_.reset();
48 static LayerImpl* FindRootScrollLayerRecursive(LayerImpl* layer) {
49 if (!layer)
50 return NULL;
52 if (layer->scrollable())
53 return layer;
55 for (size_t i = 0; i < layer->children().size(); ++i) {
56 LayerImpl* found = FindRootScrollLayerRecursive(layer->children()[i]);
57 if (found)
58 return found;
61 return NULL;
64 void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
65 if (root_scroll_layer_)
66 root_scroll_layer_->SetScrollOffsetDelegate(NULL);
67 root_layer_ = layer.Pass();
68 currently_scrolling_layer_ = NULL;
69 root_scroll_layer_ = NULL;
71 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
74 void LayerTreeImpl::FindRootScrollLayer() {
75 root_scroll_layer_ = FindRootScrollLayerRecursive(root_layer_.get());
77 if (root_scroll_layer_) {
78 UpdateMaxScrollOffset();
79 root_scroll_layer_->SetScrollOffsetDelegate(
80 root_layer_scroll_offset_delegate_);
83 if (scrolling_layer_id_from_previous_tree_) {
84 currently_scrolling_layer_ = LayerTreeHostCommon::FindLayerInSubtree(
85 root_layer_.get(),
86 scrolling_layer_id_from_previous_tree_);
89 scrolling_layer_id_from_previous_tree_ = 0;
92 scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
93 // Clear all data structures that have direct references to the layer tree.
94 scrolling_layer_id_from_previous_tree_ =
95 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
96 if (root_scroll_layer_)
97 root_scroll_layer_->SetScrollOffsetDelegate(NULL);
98 root_scroll_layer_ = NULL;
99 currently_scrolling_layer_ = NULL;
101 render_surface_layer_list_.clear();
102 set_needs_update_draw_properties();
103 return root_layer_.Pass();
106 void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl* target_tree) {
107 // The request queue should have been processed and does not require a push.
108 DCHECK_EQ(ui_resource_request_queue_.size(), 0u);
110 target_tree->SetLatencyInfo(latency_info_);
111 latency_info_.Clear();
112 target_tree->SetPageScaleFactorAndLimits(
113 page_scale_factor(), min_page_scale_factor(), max_page_scale_factor());
114 target_tree->SetPageScaleDelta(
115 target_tree->page_scale_delta() / target_tree->sent_page_scale_delta());
116 target_tree->set_sent_page_scale_delta(1);
118 // This should match the property synchronization in
119 // LayerTreeHost::finishCommitOnImplThread().
120 target_tree->set_source_frame_number(source_frame_number());
121 target_tree->set_background_color(background_color());
122 target_tree->set_has_transparent_background(has_transparent_background());
124 if (ContentsTexturesPurged())
125 target_tree->SetContentsTexturesPurged();
126 else
127 target_tree->ResetContentsTexturesPurged();
129 if (ViewportSizeInvalid())
130 target_tree->SetViewportSizeInvalid();
131 else
132 target_tree->ResetViewportSizeInvalid();
134 if (hud_layer())
135 target_tree->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl*>(
136 LayerTreeHostCommon::FindLayerInSubtree(
137 target_tree->root_layer(), hud_layer()->id())));
138 else
139 target_tree->set_hud_layer(NULL);
142 LayerImpl* LayerTreeImpl::RootScrollLayer() const {
143 return root_scroll_layer_;
146 LayerImpl* LayerTreeImpl::RootContainerLayer() const {
147 return root_scroll_layer_ ? root_scroll_layer_->parent() : NULL;
150 LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() const {
151 DCHECK(IsActiveTree());
152 return currently_scrolling_layer_;
155 void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl* layer) {
156 if (currently_scrolling_layer_ == layer)
157 return;
159 if (currently_scrolling_layer_ &&
160 currently_scrolling_layer_->scrollbar_animation_controller())
161 currently_scrolling_layer_->scrollbar_animation_controller()->
162 DidScrollGestureEnd(CurrentPhysicalTimeTicks());
163 currently_scrolling_layer_ = layer;
164 if (layer && layer->scrollbar_animation_controller())
165 layer->scrollbar_animation_controller()->DidScrollGestureBegin();
168 void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
169 SetCurrentlyScrollingLayer(NULL);
170 scrolling_layer_id_from_previous_tree_ = 0;
173 void LayerTreeImpl::SetPageScaleFactorAndLimits(float page_scale_factor,
174 float min_page_scale_factor, float max_page_scale_factor) {
175 if (!page_scale_factor)
176 return;
178 min_page_scale_factor_ = min_page_scale_factor;
179 max_page_scale_factor_ = max_page_scale_factor;
180 page_scale_factor_ = page_scale_factor;
183 void LayerTreeImpl::SetPageScaleDelta(float delta) {
184 // Clamp to the current min/max limits.
185 float total = page_scale_factor_ * delta;
186 if (min_page_scale_factor_ && total < min_page_scale_factor_)
187 delta = min_page_scale_factor_ / page_scale_factor_;
188 else if (max_page_scale_factor_ && total > max_page_scale_factor_)
189 delta = max_page_scale_factor_ / page_scale_factor_;
191 if (delta == page_scale_delta_)
192 return;
194 page_scale_delta_ = delta;
196 if (IsActiveTree()) {
197 LayerTreeImpl* pending_tree = layer_tree_host_impl_->pending_tree();
198 if (pending_tree) {
199 DCHECK_EQ(1, pending_tree->sent_page_scale_delta());
200 pending_tree->SetPageScaleDelta(
201 page_scale_delta_ / sent_page_scale_delta_);
205 UpdateMaxScrollOffset();
206 set_needs_update_draw_properties();
209 gfx::SizeF LayerTreeImpl::ScrollableViewportSize() const {
210 return gfx::ScaleSize(layer_tree_host_impl_->VisibleViewportSize(),
211 1.0f / total_page_scale_factor());
214 void LayerTreeImpl::UpdateMaxScrollOffset() {
215 LayerImpl* root_scroll = RootScrollLayer();
216 if (!root_scroll || !root_scroll->children().size())
217 return;
219 gfx::Vector2dF max_scroll = gfx::Rect(ScrollableSize()).bottom_right() -
220 gfx::RectF(ScrollableViewportSize()).bottom_right();
222 // The viewport may be larger than the contents in some cases, such as
223 // having a vertical scrollbar but no horizontal overflow.
224 max_scroll.SetToMax(gfx::Vector2dF());
226 root_scroll_layer_->SetMaxScrollOffset(gfx::ToFlooredVector2d(max_scroll));
229 static void ApplySentScrollDeltasFromAbortedCommitTo(LayerImpl* layer) {
230 layer->ApplySentScrollDeltasFromAbortedCommit();
233 void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
234 DCHECK(IsActiveTree());
236 page_scale_factor_ *= sent_page_scale_delta_;
237 page_scale_delta_ /= sent_page_scale_delta_;
238 sent_page_scale_delta_ = 1.f;
240 if (!root_layer())
241 return;
243 LayerTreeHostCommon::CallFunctionForSubtree(
244 root_layer(), base::Bind(&ApplySentScrollDeltasFromAbortedCommitTo));
247 static void ApplyScrollDeltasSinceBeginFrameTo(LayerImpl* layer) {
248 layer->ApplyScrollDeltasSinceBeginFrame();
251 void LayerTreeImpl::ApplyScrollDeltasSinceBeginFrame() {
252 DCHECK(IsPendingTree());
253 if (!root_layer())
254 return;
256 LayerTreeHostCommon::CallFunctionForSubtree(
257 root_layer(), base::Bind(&ApplyScrollDeltasSinceBeginFrameTo));
260 void LayerTreeImpl::UpdateSolidColorScrollbars() {
261 DCHECK(settings().solid_color_scrollbars);
263 LayerImpl* root_scroll = RootScrollLayer();
264 DCHECK(root_scroll);
265 DCHECK(IsActiveTree());
267 gfx::RectF scrollable_viewport(
268 gfx::PointAtOffsetFromOrigin(root_scroll->TotalScrollOffset()),
269 ScrollableViewportSize());
270 float vertical_adjust = 0.0f;
271 if (RootContainerLayer())
272 vertical_adjust = layer_tree_host_impl_->VisibleViewportSize().height() -
273 RootContainerLayer()->bounds().height();
274 if (PaintedScrollbarLayerImpl* horiz =
275 root_scroll->horizontal_scrollbar_layer()) {
276 horiz->SetVerticalAdjust(vertical_adjust);
277 horiz->SetVisibleToTotalLengthRatio(
278 scrollable_viewport.width() / ScrollableSize().width());
280 if (PaintedScrollbarLayerImpl* vertical =
281 root_scroll->vertical_scrollbar_layer()) {
282 vertical->SetVerticalAdjust(vertical_adjust);
283 vertical->SetVisibleToTotalLengthRatio(
284 scrollable_viewport.height() / ScrollableSize().height());
288 void LayerTreeImpl::UpdateDrawProperties() {
289 if (IsActiveTree() && RootScrollLayer() && RootContainerLayer())
290 UpdateRootScrollLayerSizeDelta();
292 if (settings().solid_color_scrollbars &&
293 IsActiveTree() &&
294 RootScrollLayer()) {
295 UpdateSolidColorScrollbars();
298 needs_update_draw_properties_ = false;
299 render_surface_layer_list_.clear();
301 // For max_texture_size.
302 if (!layer_tree_host_impl_->renderer())
303 return;
305 if (!root_layer())
306 return;
309 TRACE_EVENT2("cc",
310 "LayerTreeImpl::UpdateDrawProperties",
311 "IsActive",
312 IsActiveTree(),
313 "SourceFrameNumber",
314 source_frame_number_);
315 LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
316 root_layer(),
317 layer_tree_host_impl_->DeviceViewport().size(),
318 layer_tree_host_impl_->DeviceTransform(),
319 device_scale_factor(),
320 total_page_scale_factor(),
321 root_scroll_layer_ ? root_scroll_layer_->parent() : NULL,
322 MaxTextureSize(),
323 settings().can_use_lcd_text,
324 settings().layer_transforms_should_scale_layer_contents,
325 &render_surface_layer_list_);
326 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
329 DCHECK(!needs_update_draw_properties_) <<
330 "CalcDrawProperties should not set_needs_update_draw_properties()";
333 const LayerImplList& LayerTreeImpl::RenderSurfaceLayerList() const {
334 // If this assert triggers, then the list is dirty.
335 DCHECK(!needs_update_draw_properties_);
336 return render_surface_layer_list_;
339 gfx::Size LayerTreeImpl::ScrollableSize() const {
340 if (!root_scroll_layer_ || root_scroll_layer_->children().empty())
341 return gfx::Size();
342 return root_scroll_layer_->children()[0]->bounds();
345 LayerImpl* LayerTreeImpl::LayerById(int id) {
346 LayerIdMap::iterator iter = layer_id_map_.find(id);
347 return iter != layer_id_map_.end() ? iter->second : NULL;
350 void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
351 DCHECK(!LayerById(layer->id()));
352 layer_id_map_[layer->id()] = layer;
355 void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
356 DCHECK(LayerById(layer->id()));
357 layer_id_map_.erase(layer->id());
360 void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pending_tree) {
361 pending_tree->SetCurrentlyScrollingLayer(
362 LayerTreeHostCommon::FindLayerInSubtree(pending_tree->root_layer(),
363 currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0));
364 pending_tree->SetLatencyInfo(latency_info_);
365 latency_info_.Clear();
368 static void DidBecomeActiveRecursive(LayerImpl* layer) {
369 layer->DidBecomeActive();
370 for (size_t i = 0; i < layer->children().size(); ++i)
371 DidBecomeActiveRecursive(layer->children()[i]);
374 void LayerTreeImpl::DidBecomeActive() {
375 if (!root_layer())
376 return;
378 DidBecomeActiveRecursive(root_layer());
379 FindRootScrollLayer();
382 bool LayerTreeImpl::ContentsTexturesPurged() const {
383 return contents_textures_purged_;
386 void LayerTreeImpl::SetContentsTexturesPurged() {
387 if (contents_textures_purged_)
388 return;
389 contents_textures_purged_ = true;
390 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
393 void LayerTreeImpl::ResetContentsTexturesPurged() {
394 if (!contents_textures_purged_)
395 return;
396 contents_textures_purged_ = false;
397 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
400 bool LayerTreeImpl::ViewportSizeInvalid() const {
401 return viewport_size_invalid_;
404 void LayerTreeImpl::SetViewportSizeInvalid() {
405 viewport_size_invalid_ = true;
406 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
409 void LayerTreeImpl::ResetViewportSizeInvalid() {
410 viewport_size_invalid_ = false;
411 layer_tree_host_impl_->OnCanDrawStateChangedForTree();
414 Proxy* LayerTreeImpl::proxy() const {
415 return layer_tree_host_impl_->proxy();
418 const LayerTreeSettings& LayerTreeImpl::settings() const {
419 return layer_tree_host_impl_->settings();
422 const RendererCapabilities& LayerTreeImpl::GetRendererCapabilities() const {
423 return layer_tree_host_impl_->GetRendererCapabilities();
426 ContextProvider* LayerTreeImpl::context_provider() const {
427 return output_surface()->context_provider();
430 OutputSurface* LayerTreeImpl::output_surface() const {
431 return layer_tree_host_impl_->output_surface();
434 ResourceProvider* LayerTreeImpl::resource_provider() const {
435 return layer_tree_host_impl_->resource_provider();
438 TileManager* LayerTreeImpl::tile_manager() const {
439 return layer_tree_host_impl_->tile_manager();
442 FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
443 return layer_tree_host_impl_->fps_counter();
446 PaintTimeCounter* LayerTreeImpl::paint_time_counter() const {
447 return layer_tree_host_impl_->paint_time_counter();
450 MemoryHistory* LayerTreeImpl::memory_history() const {
451 return layer_tree_host_impl_->memory_history();
454 bool LayerTreeImpl::IsActiveTree() const {
455 return layer_tree_host_impl_->active_tree() == this;
458 bool LayerTreeImpl::IsPendingTree() const {
459 return layer_tree_host_impl_->pending_tree() == this;
462 bool LayerTreeImpl::IsRecycleTree() const {
463 return layer_tree_host_impl_->recycle_tree() == this;
466 LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
467 LayerTreeImpl* tree = layer_tree_host_impl_->active_tree();
468 if (!tree)
469 return NULL;
470 return tree->LayerById(id);
473 LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
474 LayerTreeImpl* tree = layer_tree_host_impl_->pending_tree();
475 if (!tree)
476 return NULL;
477 return tree->LayerById(id);
480 int LayerTreeImpl::MaxTextureSize() const {
481 return layer_tree_host_impl_->GetRendererCapabilities().max_texture_size;
484 bool LayerTreeImpl::PinchGestureActive() const {
485 return layer_tree_host_impl_->pinch_gesture_active();
488 base::TimeTicks LayerTreeImpl::CurrentFrameTimeTicks() const {
489 return layer_tree_host_impl_->CurrentFrameTimeTicks();
492 base::Time LayerTreeImpl::CurrentFrameTime() const {
493 return layer_tree_host_impl_->CurrentFrameTime();
496 base::TimeTicks LayerTreeImpl::CurrentPhysicalTimeTicks() const {
497 return layer_tree_host_impl_->CurrentPhysicalTimeTicks();
500 void LayerTreeImpl::SetNeedsCommit() {
501 layer_tree_host_impl_->SetNeedsCommit();
504 void LayerTreeImpl::SetNeedsRedraw() {
505 layer_tree_host_impl_->SetNeedsRedraw();
508 const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
509 return layer_tree_host_impl_->debug_state();
512 float LayerTreeImpl::device_scale_factor() const {
513 return layer_tree_host_impl_->device_scale_factor();
516 gfx::Size LayerTreeImpl::device_viewport_size() const {
517 return layer_tree_host_impl_->device_viewport_size();
520 DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
521 return layer_tree_host_impl_->debug_rect_history();
524 AnimationRegistrar* LayerTreeImpl::animationRegistrar() const {
525 return layer_tree_host_impl_->animation_registrar();
528 scoped_ptr<base::Value> LayerTreeImpl::AsValue() const {
529 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
530 TracedValue::MakeDictIntoImplicitSnapshot(
531 state.get(), "cc::LayerTreeImpl", this);
533 state->Set("root_layer", root_layer_->AsValue().release());
535 scoped_ptr<base::ListValue> render_surface_layer_list(new base::ListValue());
536 typedef LayerIterator<LayerImpl,
537 LayerImplList,
538 RenderSurfaceImpl,
539 LayerIteratorActions::BackToFront> LayerIteratorType;
540 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
541 for (LayerIteratorType it = LayerIteratorType::Begin(
542 &render_surface_layer_list_); it != end; ++it) {
543 if (!it.represents_itself())
544 continue;
545 render_surface_layer_list->Append(TracedValue::CreateIDRef(*it).release());
548 state->Set("render_surface_layer_list",
549 render_surface_layer_list.release());
550 return state.PassAs<base::Value>();
553 void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
554 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
555 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
556 if (root_scroll_layer_) {
557 root_scroll_layer_->SetScrollOffsetDelegate(
558 root_layer_scroll_offset_delegate_);
562 void LayerTreeImpl::UpdateRootScrollLayerSizeDelta() {
563 LayerImpl* root_scroll = RootScrollLayer();
564 LayerImpl* root_container = RootContainerLayer();
565 DCHECK(root_scroll);
566 DCHECK(root_container);
567 DCHECK(IsActiveTree());
569 gfx::Vector2dF scrollable_viewport_size =
570 gfx::RectF(ScrollableViewportSize()).bottom_right() - gfx::PointF();
572 gfx::Vector2dF original_viewport_size =
573 gfx::RectF(root_container->bounds()).bottom_right() -
574 gfx::PointF();
575 original_viewport_size.Scale(1 / page_scale_factor());
577 root_scroll->SetFixedContainerSizeDelta(
578 scrollable_viewport_size - original_viewport_size);
581 void LayerTreeImpl::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
582 latency_info_.MergeWith(latency_info);
585 const ui::LatencyInfo& LayerTreeImpl::GetLatencyInfo() {
586 return latency_info_;
589 void LayerTreeImpl::ClearLatencyInfo() {
590 latency_info_.Clear();
593 void LayerTreeImpl::WillModifyTilePriorities() {
594 layer_tree_host_impl_->SetNeedsManageTiles();
597 void LayerTreeImpl::set_ui_resource_request_queue(
598 const UIResourceRequestQueue& queue) {
599 ui_resource_request_queue_ = queue;
602 ResourceProvider::ResourceId LayerTreeImpl::ResourceIdForUIResource(
603 UIResourceId uid) const {
604 return layer_tree_host_impl_->ResourceIdForUIResource(uid);
607 void LayerTreeImpl::ProcessUIResourceRequestQueue() {
608 while (ui_resource_request_queue_.size() > 0) {
609 UIResourceRequest req = ui_resource_request_queue_.front();
610 ui_resource_request_queue_.pop_front();
612 switch (req.type) {
613 case UIResourceRequest::UIResourceCreate:
614 layer_tree_host_impl_->CreateUIResource(req.id, req.bitmap);
615 break;
616 case UIResourceRequest::UIResourceDelete:
617 layer_tree_host_impl_->DeleteUIResource(req.id);
618 break;
619 default:
620 NOTREACHED();
621 break;
626 void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl* layer) {
627 // Only the active tree needs to know about layers with copy requests, as
628 // they are aborted if not serviced during draw.
629 DCHECK(IsActiveTree());
631 DCHECK(std::find(layers_with_copy_output_request_.begin(),
632 layers_with_copy_output_request_.end(),
633 layer) == layers_with_copy_output_request_.end());
634 layers_with_copy_output_request_.push_back(layer);
637 void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl* layer) {
638 // Only the active tree needs to know about layers with copy requests, as
639 // they are aborted if not serviced during draw.
640 DCHECK(IsActiveTree());
642 std::vector<LayerImpl*>::iterator it = std::find(
643 layers_with_copy_output_request_.begin(),
644 layers_with_copy_output_request_.end(),
645 layer);
646 DCHECK(it != layers_with_copy_output_request_.end());
647 layers_with_copy_output_request_.erase(it);
650 const std::vector<LayerImpl*> LayerTreeImpl::LayersWithCopyOutputRequest()
651 const {
652 // Only the active tree needs to know about layers with copy requests, as
653 // they are aborted if not serviced during draw.
654 DCHECK(IsActiveTree());
656 return layers_with_copy_output_request_;
659 } // namespace cc