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