- add sources.
[platform/framework/web/crosswalk.git] / src / cc / layers / layer.cc
1 // Copyright 2010 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/layers/layer.h"
6
7 #include <algorithm>
8
9 #include "base/debug/trace_event.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "cc/animation/animation.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/layer_animation_controller.h"
16 #include "cc/layers/layer_client.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/output/copy_output_request.h"
19 #include "cc/output/copy_output_result.h"
20 #include "cc/trees/layer_tree_host.h"
21 #include "cc/trees/layer_tree_impl.h"
22 #include "third_party/skia/include/core/SkImageFilter.h"
23 #include "ui/gfx/rect_conversions.h"
24
25 namespace cc {
26
27 static int s_next_layer_id = 1;
28
29 scoped_refptr<Layer> Layer::Create() {
30   return make_scoped_refptr(new Layer());
31 }
32
33 Layer::Layer()
34     : needs_push_properties_(false),
35       num_dependents_need_push_properties_(false),
36       stacking_order_changed_(false),
37       layer_id_(s_next_layer_id++),
38       ignore_set_needs_commit_(false),
39       parent_(NULL),
40       layer_tree_host_(NULL),
41       scrollable_(false),
42       should_scroll_on_main_thread_(false),
43       have_wheel_event_handlers_(false),
44       user_scrollable_horizontal_(true),
45       user_scrollable_vertical_(true),
46       anchor_point_(0.5f, 0.5f),
47       background_color_(0),
48       compositing_reasons_(kCompositingReasonUnknown),
49       opacity_(1.f),
50       anchor_point_z_(0.f),
51       is_container_for_fixed_position_layers_(false),
52       is_drawable_(false),
53       hide_layer_and_subtree_(false),
54       masks_to_bounds_(false),
55       contents_opaque_(false),
56       double_sided_(true),
57       preserves_3d_(false),
58       use_parent_backface_visibility_(false),
59       draw_checkerboard_for_missing_tiles_(false),
60       force_render_surface_(false),
61       scroll_parent_(NULL),
62       clip_parent_(NULL),
63       replica_layer_(NULL),
64       raster_scale_(0.f),
65       client_(NULL) {
66   if (layer_id_ < 0) {
67     s_next_layer_id = 1;
68     layer_id_ = s_next_layer_id++;
69   }
70
71   layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
72   layer_animation_controller_->AddValueObserver(this);
73 }
74
75 Layer::~Layer() {
76   // Our parent should be holding a reference to us so there should be no
77   // way for us to be destroyed while we still have a parent.
78   DCHECK(!parent());
79   // Similarly we shouldn't have a layer tree host since it also keeps a
80   // reference to us.
81   DCHECK(!layer_tree_host());
82
83   layer_animation_controller_->RemoveValueObserver(this);
84
85   // Remove the parent reference from all children and dependents.
86   RemoveAllChildren();
87   if (mask_layer_.get()) {
88     DCHECK_EQ(this, mask_layer_->parent());
89     mask_layer_->RemoveFromParent();
90   }
91   if (replica_layer_.get()) {
92     DCHECK_EQ(this, replica_layer_->parent());
93     replica_layer_->RemoveFromParent();
94   }
95
96   RemoveFromScrollTree();
97   RemoveFromClipTree();
98 }
99
100 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
101   if (layer_tree_host_ == host)
102     return;
103
104   layer_tree_host_ = host;
105
106   // When changing hosts, the layer needs to commit its properties to the impl
107   // side for the new host.
108   SetNeedsPushProperties();
109
110   for (size_t i = 0; i < children_.size(); ++i)
111     children_[i]->SetLayerTreeHost(host);
112
113   if (mask_layer_.get())
114     mask_layer_->SetLayerTreeHost(host);
115   if (replica_layer_.get())
116     replica_layer_->SetLayerTreeHost(host);
117
118   if (host) {
119     layer_animation_controller_->SetAnimationRegistrar(
120         host->animation_registrar());
121
122     if (host->settings().layer_transforms_should_scale_layer_contents)
123       reset_raster_scale_to_unknown();
124   }
125
126   if (host && layer_animation_controller_->has_any_animation())
127     host->SetNeedsCommit();
128   if (host && (!filters_.IsEmpty() || !background_filters_.IsEmpty()))
129     layer_tree_host_->set_needs_filter_context();
130 }
131
132 void Layer::SetNeedsUpdate() {
133   if (layer_tree_host_ && !ignore_set_needs_commit_)
134     layer_tree_host_->SetNeedsUpdateLayers();
135 }
136
137 void Layer::SetNeedsCommit() {
138   if (!layer_tree_host_)
139     return;
140
141   SetNeedsPushProperties();
142
143   if (ignore_set_needs_commit_)
144     return;
145
146   layer_tree_host_->SetNeedsCommit();
147 }
148
149 void Layer::SetNeedsFullTreeSync() {
150   if (!layer_tree_host_)
151     return;
152
153   layer_tree_host_->SetNeedsFullTreeSync();
154 }
155
156 void Layer::SetNextCommitWaitsForActivation() {
157   if (!layer_tree_host_)
158     return;
159
160   layer_tree_host_->SetNextCommitWaitsForActivation();
161 }
162
163 void Layer::SetNeedsPushProperties() {
164   if (needs_push_properties_)
165     return;
166   if (!parent_should_know_need_push_properties() && parent_)
167     parent_->AddDependentNeedsPushProperties();
168   needs_push_properties_ = true;
169 }
170
171 void Layer::AddDependentNeedsPushProperties() {
172   DCHECK_GE(num_dependents_need_push_properties_, 0);
173
174   if (!parent_should_know_need_push_properties() && parent_)
175     parent_->AddDependentNeedsPushProperties();
176
177   num_dependents_need_push_properties_++;
178 }
179
180 void Layer::RemoveDependentNeedsPushProperties() {
181   num_dependents_need_push_properties_--;
182   DCHECK_GE(num_dependents_need_push_properties_, 0);
183
184   if (!parent_should_know_need_push_properties() && parent_)
185       parent_->RemoveDependentNeedsPushProperties();
186 }
187
188 bool Layer::IsPropertyChangeAllowed() const {
189   if (!layer_tree_host_)
190     return true;
191
192   if (!layer_tree_host_->settings().strict_layer_property_change_checking)
193     return true;
194
195   return !layer_tree_host_->in_paint_layer_contents();
196 }
197
198 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const {
199   gfx::RectF content_rect =
200       gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
201   // Intersect with content rect to avoid the extra pixel because for some
202   // values x and y, ceil((x / y) * y) may be x + 1.
203   content_rect.Intersect(gfx::Rect(content_bounds()));
204   return gfx::ToEnclosingRect(content_rect);
205 }
206
207 skia::RefPtr<SkPicture> Layer::GetPicture() const {
208   return skia::RefPtr<SkPicture>();
209 }
210
211 void Layer::SetParent(Layer* layer) {
212   DCHECK(!layer || !layer->HasAncestor(this));
213
214   if (parent_should_know_need_push_properties()) {
215     if (parent_)
216       parent_->RemoveDependentNeedsPushProperties();
217     if (layer)
218       layer->AddDependentNeedsPushProperties();
219   }
220
221   parent_ = layer;
222   SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL);
223
224   if (!layer_tree_host_)
225     return;
226   const LayerTreeSettings& settings = layer_tree_host_->settings();
227   if (!settings.layer_transforms_should_scale_layer_contents)
228     return;
229
230   reset_raster_scale_to_unknown();
231   if (mask_layer_.get())
232     mask_layer_->reset_raster_scale_to_unknown();
233   if (replica_layer_.get() && replica_layer_->mask_layer_.get())
234     replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
235 }
236
237 void Layer::AddChild(scoped_refptr<Layer> child) {
238   InsertChild(child, children_.size());
239 }
240
241 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
242   DCHECK(IsPropertyChangeAllowed());
243   child->RemoveFromParent();
244   child->SetParent(this);
245   child->stacking_order_changed_ = true;
246
247   index = std::min(index, children_.size());
248   children_.insert(children_.begin() + index, child);
249   SetNeedsFullTreeSync();
250 }
251
252 void Layer::RemoveFromParent() {
253   DCHECK(IsPropertyChangeAllowed());
254   if (parent_)
255     parent_->RemoveChildOrDependent(this);
256 }
257
258 void Layer::RemoveChildOrDependent(Layer* child) {
259   if (mask_layer_.get() == child) {
260     mask_layer_->SetParent(NULL);
261     mask_layer_ = NULL;
262     SetNeedsFullTreeSync();
263     return;
264   }
265   if (replica_layer_.get() == child) {
266     replica_layer_->SetParent(NULL);
267     replica_layer_ = NULL;
268     SetNeedsFullTreeSync();
269     return;
270   }
271
272   for (LayerList::iterator iter = children_.begin();
273        iter != children_.end();
274        ++iter) {
275     if (iter->get() != child)
276       continue;
277
278     child->SetParent(NULL);
279     children_.erase(iter);
280     SetNeedsFullTreeSync();
281     return;
282   }
283 }
284
285 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
286   DCHECK(reference);
287   DCHECK_EQ(reference->parent(), this);
288   DCHECK(IsPropertyChangeAllowed());
289
290   if (reference == new_layer.get())
291     return;
292
293   int reference_index = IndexOfChild(reference);
294   if (reference_index == -1) {
295     NOTREACHED();
296     return;
297   }
298
299   reference->RemoveFromParent();
300
301   if (new_layer.get()) {
302     new_layer->RemoveFromParent();
303     InsertChild(new_layer, reference_index);
304   }
305 }
306
307 int Layer::IndexOfChild(const Layer* reference) {
308   for (size_t i = 0; i < children_.size(); ++i) {
309     if (children_[i].get() == reference)
310       return i;
311   }
312   return -1;
313 }
314
315 void Layer::SetBounds(gfx::Size size) {
316   DCHECK(IsPropertyChangeAllowed());
317   if (bounds() == size)
318     return;
319
320   bounds_ = size;
321   SetNeedsCommit();
322 }
323
324 Layer* Layer::RootLayer() {
325   Layer* layer = this;
326   while (layer->parent())
327     layer = layer->parent();
328   return layer;
329 }
330
331 void Layer::RemoveAllChildren() {
332   DCHECK(IsPropertyChangeAllowed());
333   while (children_.size()) {
334     Layer* layer = children_[0].get();
335     DCHECK_EQ(this, layer->parent());
336     layer->RemoveFromParent();
337   }
338 }
339
340 void Layer::SetChildren(const LayerList& children) {
341   DCHECK(IsPropertyChangeAllowed());
342   if (children == children_)
343     return;
344
345   RemoveAllChildren();
346   for (size_t i = 0; i < children.size(); ++i)
347     AddChild(children[i]);
348 }
349
350 bool Layer::HasAncestor(const Layer* ancestor) const {
351   for (const Layer* layer = parent(); layer; layer = layer->parent()) {
352     if (layer == ancestor)
353       return true;
354   }
355   return false;
356 }
357
358 void Layer::RequestCopyOfOutput(
359     scoped_ptr<CopyOutputRequest> request) {
360   DCHECK(IsPropertyChangeAllowed());
361   if (request->IsEmpty())
362     return;
363   copy_requests_.push_back(request.Pass());
364   SetNeedsCommit();
365 }
366
367 void Layer::SetAnchorPoint(gfx::PointF anchor_point) {
368   DCHECK(IsPropertyChangeAllowed());
369   if (anchor_point_ == anchor_point)
370     return;
371   anchor_point_ = anchor_point;
372   SetNeedsCommit();
373 }
374
375 void Layer::SetAnchorPointZ(float anchor_point_z) {
376   DCHECK(IsPropertyChangeAllowed());
377   if (anchor_point_z_ == anchor_point_z)
378     return;
379   anchor_point_z_ = anchor_point_z;
380   SetNeedsCommit();
381 }
382
383 void Layer::SetBackgroundColor(SkColor background_color) {
384   DCHECK(IsPropertyChangeAllowed());
385   if (background_color_ == background_color)
386     return;
387   background_color_ = background_color;
388   SetNeedsCommit();
389 }
390
391 SkColor Layer::SafeOpaqueBackgroundColor() const {
392   SkColor color = background_color();
393   if (SkColorGetA(color) == 255 && !contents_opaque()) {
394     color = SK_ColorTRANSPARENT;
395   } else if (SkColorGetA(color) != 255 && contents_opaque()) {
396     for (const Layer* layer = parent(); layer;
397          layer = layer->parent()) {
398       color = layer->background_color();
399       if (SkColorGetA(color) == 255)
400         break;
401     }
402     if (SkColorGetA(color) != 255)
403       color = layer_tree_host_->background_color();
404     if (SkColorGetA(color) != 255)
405       color = SkColorSetA(color, 255);
406   }
407   return color;
408 }
409
410 void Layer::CalculateContentsScale(
411     float ideal_contents_scale,
412     float device_scale_factor,
413     float page_scale_factor,
414     bool animating_transform_to_screen,
415     float* contents_scale_x,
416     float* contents_scale_y,
417     gfx::Size* content_bounds) {
418   DCHECK(layer_tree_host_);
419
420   *contents_scale_x = 1;
421   *contents_scale_y = 1;
422   *content_bounds = bounds();
423 }
424
425 void Layer::SetMasksToBounds(bool masks_to_bounds) {
426   DCHECK(IsPropertyChangeAllowed());
427   if (masks_to_bounds_ == masks_to_bounds)
428     return;
429   masks_to_bounds_ = masks_to_bounds;
430   SetNeedsCommit();
431 }
432
433 void Layer::SetMaskLayer(Layer* mask_layer) {
434   DCHECK(IsPropertyChangeAllowed());
435   if (mask_layer_.get() == mask_layer)
436     return;
437   if (mask_layer_.get()) {
438     DCHECK_EQ(this, mask_layer_->parent());
439     mask_layer_->RemoveFromParent();
440   }
441   mask_layer_ = mask_layer;
442   if (mask_layer_.get()) {
443     DCHECK(!mask_layer_->parent());
444     mask_layer_->RemoveFromParent();
445     mask_layer_->SetParent(this);
446     mask_layer_->SetIsMask(true);
447   }
448   SetNeedsFullTreeSync();
449 }
450
451 void Layer::SetReplicaLayer(Layer* layer) {
452   DCHECK(IsPropertyChangeAllowed());
453   if (replica_layer_.get() == layer)
454     return;
455   if (replica_layer_.get()) {
456     DCHECK_EQ(this, replica_layer_->parent());
457     replica_layer_->RemoveFromParent();
458   }
459   replica_layer_ = layer;
460   if (replica_layer_.get()) {
461     DCHECK(!replica_layer_->parent());
462     replica_layer_->RemoveFromParent();
463     replica_layer_->SetParent(this);
464   }
465   SetNeedsFullTreeSync();
466 }
467
468 void Layer::SetFilters(const FilterOperations& filters) {
469   DCHECK(IsPropertyChangeAllowed());
470   if (filters_ == filters)
471     return;
472   filters_ = filters;
473   SetNeedsCommit();
474   if (!filters.IsEmpty() && layer_tree_host_)
475     layer_tree_host_->set_needs_filter_context();
476 }
477
478 bool Layer::FilterIsAnimating() const {
479   return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
480 }
481
482 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
483   DCHECK(IsPropertyChangeAllowed());
484   if (background_filters_ == filters)
485     return;
486   background_filters_ = filters;
487   SetNeedsCommit();
488   if (!filters.IsEmpty() && layer_tree_host_)
489     layer_tree_host_->set_needs_filter_context();
490 }
491
492 void Layer::SetOpacity(float opacity) {
493   DCHECK(IsPropertyChangeAllowed());
494   if (opacity_ == opacity)
495     return;
496   opacity_ = opacity;
497   SetNeedsCommit();
498 }
499
500 bool Layer::OpacityIsAnimating() const {
501   return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
502 }
503
504 bool Layer::OpacityCanAnimateOnImplThread() const {
505   return false;
506 }
507
508 void Layer::SetContentsOpaque(bool opaque) {
509   DCHECK(IsPropertyChangeAllowed());
510   if (contents_opaque_ == opaque)
511     return;
512   contents_opaque_ = opaque;
513   SetNeedsCommit();
514 }
515
516 void Layer::SetPosition(gfx::PointF position) {
517   DCHECK(IsPropertyChangeAllowed());
518   if (position_ == position)
519     return;
520   position_ = position;
521   SetNeedsCommit();
522 }
523
524 bool Layer::IsContainerForFixedPositionLayers() const {
525   if (!transform_.IsIdentityOrTranslation())
526     return true;
527   if (parent_ && !parent_->sublayer_transform_.IsIdentityOrTranslation())
528     return true;
529   return is_container_for_fixed_position_layers_;
530 }
531
532 void Layer::SetSublayerTransform(const gfx::Transform& sublayer_transform) {
533   DCHECK(IsPropertyChangeAllowed());
534   if (sublayer_transform_ == sublayer_transform)
535     return;
536   sublayer_transform_ = sublayer_transform;
537   SetNeedsCommit();
538 }
539
540 void Layer::SetTransform(const gfx::Transform& transform) {
541   DCHECK(IsPropertyChangeAllowed());
542   if (transform_ == transform)
543     return;
544   transform_ = transform;
545   SetNeedsCommit();
546 }
547
548 bool Layer::TransformIsAnimating() const {
549   return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
550 }
551
552 void Layer::SetScrollParent(Layer* parent) {
553   DCHECK(IsPropertyChangeAllowed());
554   if (scroll_parent_ == parent)
555     return;
556
557   if (scroll_parent_)
558     scroll_parent_->RemoveScrollChild(this);
559
560   scroll_parent_ = parent;
561
562   if (scroll_parent_)
563     scroll_parent_->AddScrollChild(this);
564
565   SetNeedsCommit();
566 }
567
568 void Layer::AddScrollChild(Layer* child) {
569   if (!scroll_children_)
570     scroll_children_.reset(new std::set<Layer*>);
571   scroll_children_->insert(child);
572   SetNeedsCommit();
573 }
574
575 void Layer::RemoveScrollChild(Layer* child) {
576   scroll_children_->erase(child);
577   if (scroll_children_->empty())
578     scroll_children_.reset();
579   SetNeedsCommit();
580 }
581
582 void Layer::SetClipParent(Layer* ancestor) {
583   DCHECK(IsPropertyChangeAllowed());
584   if (clip_parent_ == ancestor)
585     return;
586
587   if (clip_parent_)
588     clip_parent_->RemoveClipChild(this);
589
590   clip_parent_ = ancestor;
591
592   if (clip_parent_)
593     clip_parent_->AddClipChild(this);
594
595   SetNeedsCommit();
596 }
597
598 void Layer::AddClipChild(Layer* child) {
599   if (!clip_children_)
600     clip_children_.reset(new std::set<Layer*>);
601   clip_children_->insert(child);
602   SetNeedsCommit();
603 }
604
605 void Layer::RemoveClipChild(Layer* child) {
606   clip_children_->erase(child);
607   if (clip_children_->empty())
608     clip_children_.reset();
609   SetNeedsCommit();
610 }
611
612 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
613   DCHECK(IsPropertyChangeAllowed());
614   if (scroll_offset_ == scroll_offset)
615     return;
616   scroll_offset_ = scroll_offset;
617   SetNeedsCommit();
618 }
619
620 void Layer::SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset) {
621   DCHECK(IsPropertyChangeAllowed());
622   // This function only gets called during a BeginMainFrame, so there
623   // is no need to call SetNeedsUpdate here.
624   DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
625   if (scroll_offset_ == scroll_offset)
626     return;
627   scroll_offset_ = scroll_offset;
628   SetNeedsPushProperties();
629   if (!did_scroll_callback_.is_null())
630     did_scroll_callback_.Run();
631   // The callback could potentially change the layer structure:
632   // "this" may have been destroyed during the process.
633 }
634
635 void Layer::SetMaxScrollOffset(gfx::Vector2d max_scroll_offset) {
636   DCHECK(IsPropertyChangeAllowed());
637   if (max_scroll_offset_ == max_scroll_offset)
638     return;
639   max_scroll_offset_ = max_scroll_offset;
640   SetNeedsCommit();
641 }
642
643 void Layer::SetScrollable(bool scrollable) {
644   DCHECK(IsPropertyChangeAllowed());
645   if (scrollable_ == scrollable)
646     return;
647   scrollable_ = scrollable;
648   SetNeedsCommit();
649 }
650
651 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
652   DCHECK(IsPropertyChangeAllowed());
653   if (user_scrollable_horizontal_ == horizontal &&
654       user_scrollable_vertical_ == vertical)
655     return;
656   user_scrollable_horizontal_ = horizontal;
657   user_scrollable_vertical_ = vertical;
658   SetNeedsCommit();
659 }
660
661 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
662   DCHECK(IsPropertyChangeAllowed());
663   if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
664     return;
665   should_scroll_on_main_thread_ = should_scroll_on_main_thread;
666   SetNeedsCommit();
667 }
668
669 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
670   DCHECK(IsPropertyChangeAllowed());
671   if (have_wheel_event_handlers_ == have_wheel_event_handlers)
672     return;
673   have_wheel_event_handlers_ = have_wheel_event_handlers;
674   SetNeedsCommit();
675 }
676
677 void Layer::SetNonFastScrollableRegion(const Region& region) {
678   DCHECK(IsPropertyChangeAllowed());
679   if (non_fast_scrollable_region_ == region)
680     return;
681   non_fast_scrollable_region_ = region;
682   SetNeedsCommit();
683 }
684
685 void Layer::SetTouchEventHandlerRegion(const Region& region) {
686   DCHECK(IsPropertyChangeAllowed());
687   if (touch_event_handler_region_ == region)
688     return;
689   touch_event_handler_region_ = region;
690   SetNeedsCommit();
691 }
692
693 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
694   DCHECK(IsPropertyChangeAllowed());
695   if (draw_checkerboard_for_missing_tiles_ == checkerboard)
696     return;
697   draw_checkerboard_for_missing_tiles_ = checkerboard;
698   SetNeedsCommit();
699 }
700
701 void Layer::SetForceRenderSurface(bool force) {
702   DCHECK(IsPropertyChangeAllowed());
703   if (force_render_surface_ == force)
704     return;
705   force_render_surface_ = force;
706   SetNeedsCommit();
707 }
708
709 void Layer::SetDoubleSided(bool double_sided) {
710   DCHECK(IsPropertyChangeAllowed());
711   if (double_sided_ == double_sided)
712     return;
713   double_sided_ = double_sided;
714   SetNeedsCommit();
715 }
716
717 void Layer::SetIsDrawable(bool is_drawable) {
718   DCHECK(IsPropertyChangeAllowed());
719   if (is_drawable_ == is_drawable)
720     return;
721
722   is_drawable_ = is_drawable;
723   SetNeedsCommit();
724 }
725
726 void Layer::SetHideLayerAndSubtree(bool hide) {
727   DCHECK(IsPropertyChangeAllowed());
728   if (hide_layer_and_subtree_ == hide)
729     return;
730
731   hide_layer_and_subtree_ = hide;
732   SetNeedsCommit();
733 }
734
735 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
736   if (dirty_rect.IsEmpty())
737     return;
738
739   SetNeedsPushProperties();
740   update_rect_.Union(dirty_rect);
741
742   if (DrawsContent())
743     SetNeedsUpdate();
744 }
745
746 bool Layer::DescendantIsFixedToContainerLayer() const {
747   for (size_t i = 0; i < children_.size(); ++i) {
748     if (children_[i]->position_constraint_.is_fixed_position() ||
749         children_[i]->DescendantIsFixedToContainerLayer())
750       return true;
751   }
752   return false;
753 }
754
755 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
756   if (is_container_for_fixed_position_layers_ == container)
757     return;
758   is_container_for_fixed_position_layers_ = container;
759
760   if (layer_tree_host_ && layer_tree_host_->CommitRequested())
761     return;
762
763   // Only request a commit if we have a fixed positioned descendant.
764   if (DescendantIsFixedToContainerLayer())
765     SetNeedsCommit();
766 }
767
768 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
769   DCHECK(IsPropertyChangeAllowed());
770   if (position_constraint_ == constraint)
771     return;
772   position_constraint_ = constraint;
773   SetNeedsCommit();
774 }
775
776 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
777                                         scoped_ptr<CopyOutputResult> result) {
778   request->SendResult(result.Pass());
779 }
780
781 static void PostCopyCallbackToMainThread(
782     scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
783     scoped_ptr<CopyOutputRequest> request,
784     scoped_ptr<CopyOutputResult> result) {
785   main_thread_task_runner->PostTask(FROM_HERE,
786                                     base::Bind(&RunCopyCallbackOnMainThread,
787                                                base::Passed(&request),
788                                                base::Passed(&result)));
789 }
790
791 void Layer::PushPropertiesTo(LayerImpl* layer) {
792   DCHECK(layer_tree_host_);
793
794   // If we did not SavePaintProperties() for the layer this frame, then push the
795   // real property values, not the paint property values.
796   bool use_paint_properties = paint_properties_.source_frame_number ==
797                               layer_tree_host_->source_frame_number();
798
799   layer->SetAnchorPoint(anchor_point_);
800   layer->SetAnchorPointZ(anchor_point_z_);
801   layer->SetBackgroundColor(background_color_);
802   layer->SetBounds(use_paint_properties ? paint_properties_.bounds
803                                         : bounds_);
804   layer->SetContentBounds(content_bounds());
805   layer->SetContentsScale(contents_scale_x(), contents_scale_y());
806
807   bool is_tracing;
808   TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
809                                      &is_tracing);
810   if (is_tracing)
811       layer->SetDebugName(DebugName());
812   else
813       layer->SetDebugName(std::string());
814
815   layer->SetCompositingReasons(compositing_reasons_);
816   layer->SetDoubleSided(double_sided_);
817   layer->SetDrawCheckerboardForMissingTiles(
818       draw_checkerboard_for_missing_tiles_);
819   layer->SetForceRenderSurface(force_render_surface_);
820   layer->SetDrawsContent(DrawsContent());
821   layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
822   if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
823     layer->SetFilters(filters_);
824   DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
825   layer->SetBackgroundFilters(background_filters());
826   layer->SetMasksToBounds(masks_to_bounds_);
827   layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
828   layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
829   layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
830   layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
831   layer->SetContentsOpaque(contents_opaque_);
832   if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
833     layer->SetOpacity(opacity_);
834   DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
835   layer->SetPosition(position_);
836   layer->SetIsContainerForFixedPositionLayers(
837       IsContainerForFixedPositionLayers());
838   layer->SetFixedContainerSizeDelta(gfx::Vector2dF());
839   layer->SetPositionConstraint(position_constraint_);
840   layer->SetPreserves3d(preserves_3d());
841   layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
842   layer->SetSublayerTransform(sublayer_transform_);
843   if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
844     layer->SetTransform(transform_);
845   DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
846
847   layer->SetScrollable(scrollable_);
848   layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
849   layer->set_user_scrollable_vertical(user_scrollable_vertical_);
850   layer->SetMaxScrollOffset(max_scroll_offset_);
851
852   LayerImpl* scroll_parent = NULL;
853   if (scroll_parent_)
854     scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
855
856   layer->SetScrollParent(scroll_parent);
857   if (scroll_children_) {
858     std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
859     for (std::set<Layer*>::iterator it = scroll_children_->begin();
860         it != scroll_children_->end(); ++it)
861       scroll_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
862     layer->SetScrollChildren(scroll_children);
863   }
864
865   LayerImpl* clip_parent = NULL;
866   if (clip_parent_) {
867     clip_parent =
868         layer->layer_tree_impl()->LayerById(clip_parent_->id());
869   }
870
871   layer->SetClipParent(clip_parent);
872   if (clip_children_) {
873     std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
874     for (std::set<Layer*>::iterator it = clip_children_->begin();
875         it != clip_children_->end(); ++it) {
876       LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
877       DCHECK(clip_child);
878       clip_children->insert(clip_child);
879     }
880     layer->SetClipChildren(clip_children);
881   }
882
883   // Adjust the scroll delta to be just the scrolls that have happened since
884   // the BeginMainFrame was sent.  This happens for impl-side painting
885   // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
886   if (layer->layer_tree_impl()->settings().impl_side_painting) {
887     layer->SetScrollOffset(scroll_offset_);
888   } else {
889     layer->SetScrollOffsetAndDelta(
890         scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
891     layer->SetSentScrollDelta(gfx::Vector2d());
892   }
893
894   // Wrap the copy_requests_ in a PostTask to the main thread.
895   ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
896   for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
897        it != copy_requests_.end();
898        ++it) {
899     scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
900         layer_tree_host()->proxy()->MainThreadTaskRunner();
901     scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
902     const CopyOutputRequest& original_request_ref = *original_request;
903     scoped_ptr<CopyOutputRequest> main_thread_request =
904         CopyOutputRequest::CreateRelayRequest(
905             original_request_ref,
906             base::Bind(&PostCopyCallbackToMainThread,
907                        main_thread_task_runner,
908                        base::Passed(&original_request)));
909     main_thread_copy_requests.push_back(main_thread_request.Pass());
910   }
911   copy_requests_.clear();
912   layer->PassCopyRequests(&main_thread_copy_requests);
913
914   // If the main thread commits multiple times before the impl thread actually
915   // draws, then damage tracking will become incorrect if we simply clobber the
916   // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
917   // union) any update changes that have occurred on the main thread.
918   update_rect_.Union(layer->update_rect());
919   layer->set_update_rect(update_rect_);
920
921   layer->SetStackingOrderChanged(stacking_order_changed_);
922
923   layer_animation_controller_->PushAnimationUpdatesTo(
924       layer->layer_animation_controller());
925
926   // Reset any state that should be cleared for the next update.
927   stacking_order_changed_ = false;
928   update_rect_ = gfx::RectF();
929
930   // Animating layers require further push properties to clean up the animation.
931   // crbug.com/259088
932   needs_push_properties_ = layer_animation_controller_->has_any_animation();
933   num_dependents_need_push_properties_ = 0;
934 }
935
936 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
937   return LayerImpl::Create(tree_impl, layer_id_);
938 }
939
940 bool Layer::DrawsContent() const {
941   return is_drawable_;
942 }
943
944 void Layer::SavePaintProperties() {
945   DCHECK(layer_tree_host_);
946
947   // TODO(reveman): Save all layer properties that we depend on not
948   // changing until PushProperties() has been called. crbug.com/231016
949   paint_properties_.bounds = bounds_;
950   paint_properties_.source_frame_number =
951       layer_tree_host_->source_frame_number();
952 }
953
954 bool Layer::Update(ResourceUpdateQueue* queue,
955                    const OcclusionTracker* occlusion) {
956   DCHECK(layer_tree_host_);
957   DCHECK_EQ(layer_tree_host_->source_frame_number(),
958             paint_properties_.source_frame_number) <<
959       "SavePaintProperties must be called for any layer that is painted.";
960   return false;
961 }
962
963 bool Layer::NeedMoreUpdates() {
964   return false;
965 }
966
967 std::string Layer::DebugName() {
968   return client_ ? client_->DebugName() : std::string();
969 }
970
971 void Layer::SetCompositingReasons(CompositingReasons reasons) {
972   compositing_reasons_ = reasons;
973 }
974
975 void Layer::CreateRenderSurface() {
976   DCHECK(!draw_properties_.render_surface);
977   draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
978   draw_properties_.render_target = this;
979 }
980
981 void Layer::ClearRenderSurface() {
982   draw_properties_.render_surface.reset();
983 }
984
985 // On<Property>Animated is called due to an ongoing accelerated animation.
986 // Since this animation is also being run on the compositor thread, there
987 // is no need to request a commit to push this value over, so the value is
988 // set directly rather than by calling Set<Property>.
989 void Layer::OnFilterAnimated(const FilterOperations& filters) {
990   filters_ = filters;
991 }
992
993 void Layer::OnOpacityAnimated(float opacity) {
994   opacity_ = opacity;
995 }
996
997 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
998   transform_ = transform;
999 }
1000
1001 bool Layer::IsActive() const {
1002   return true;
1003 }
1004
1005 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1006   if (!layer_animation_controller_->animation_registrar())
1007     return false;
1008
1009   UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1010                         !layer_tree_host_);
1011   layer_animation_controller_->AddAnimation(animation.Pass());
1012   SetNeedsCommit();
1013   return true;
1014 }
1015
1016 void Layer::PauseAnimation(int animation_id, double time_offset) {
1017   layer_animation_controller_->PauseAnimation(animation_id, time_offset);
1018   SetNeedsCommit();
1019 }
1020
1021 void Layer::RemoveAnimation(int animation_id) {
1022   layer_animation_controller_->RemoveAnimation(animation_id);
1023   SetNeedsCommit();
1024 }
1025
1026 void Layer::SetLayerAnimationControllerForTest(
1027     scoped_refptr<LayerAnimationController> controller) {
1028   layer_animation_controller_->RemoveValueObserver(this);
1029   layer_animation_controller_ = controller;
1030   layer_animation_controller_->set_force_sync();
1031   layer_animation_controller_->AddValueObserver(this);
1032   SetNeedsCommit();
1033 }
1034
1035 bool Layer::HasActiveAnimation() const {
1036   return layer_animation_controller_->HasActiveAnimation();
1037 }
1038
1039 void Layer::AddLayerAnimationEventObserver(
1040     LayerAnimationEventObserver* animation_observer) {
1041   layer_animation_controller_->AddEventObserver(animation_observer);
1042 }
1043
1044 void Layer::RemoveLayerAnimationEventObserver(
1045     LayerAnimationEventObserver* animation_observer) {
1046   layer_animation_controller_->RemoveEventObserver(animation_observer);
1047 }
1048
1049 Region Layer::VisibleContentOpaqueRegion() const {
1050   if (contents_opaque())
1051     return visible_content_rect();
1052   return Region();
1053 }
1054
1055 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1056   return NULL;
1057 }
1058
1059 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1060   return layer_tree_host_->rendering_stats_instrumentation();
1061 }
1062
1063 bool Layer::SupportsLCDText() const {
1064   return false;
1065 }
1066
1067 void Layer::RemoveFromScrollTree() {
1068   if (scroll_children_.get()) {
1069     for (std::set<Layer*>::iterator it = scroll_children_->begin();
1070         it != scroll_children_->end(); ++it)
1071       (*it)->scroll_parent_ = NULL;
1072   }
1073
1074   if (scroll_parent_)
1075     scroll_parent_->RemoveScrollChild(this);
1076
1077   scroll_parent_ = NULL;
1078 }
1079
1080 void Layer::RemoveFromClipTree() {
1081   if (clip_children_.get()) {
1082     for (std::set<Layer*>::iterator it = clip_children_->begin();
1083         it != clip_children_->end(); ++it)
1084       (*it)->clip_parent_ = NULL;
1085   }
1086
1087   if (clip_parent_)
1088     clip_parent_->RemoveClipChild(this);
1089
1090   clip_parent_ = NULL;
1091 }
1092
1093 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1094   benchmark->RunOnLayer(this);
1095 }
1096
1097 }  // namespace cc