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