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.
7 #include "cc/animation.h"
8 #include "cc/animation_events.h"
9 #include "cc/layer_animation_controller.h"
10 #include "cc/layer_impl.h"
11 #include "cc/layer_tree_host.h"
12 #include "cc/layer_tree_impl.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegate.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayerScrollClient.h"
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
16 #include "third_party/skia/include/core/SkImageFilter.h"
17 #include "ui/gfx/rect_conversions.h"
21 static int s_nextLayerId
= 1;
23 scoped_refptr
<Layer
> Layer::create()
25 return make_scoped_refptr(new Layer());
29 : m_needsDisplay(false)
30 , m_stackingOrderChanged(false)
31 , m_layerId(s_nextLayerId
++)
32 , m_ignoreSetNeedsCommit(false)
36 , m_shouldScrollOnMainThread(false)
37 , m_haveWheelEventHandlers(false)
38 , m_anchorPoint(0.5, 0.5)
39 , m_backgroundColor(0)
42 , m_isContainerForFixedPositionLayers(false)
43 , m_fixedToContainerLayer(false)
45 , m_masksToBounds(false)
46 , m_contentsOpaque(false)
48 , m_preserves3D(false)
49 , m_useParentBackfaceVisibility(false)
50 , m_drawCheckerboardForMissingTiles(false)
51 , m_forceRenderSurface(false)
54 , m_automaticallyComputeRasterScale(false)
55 , m_boundsContainPageScale(false)
56 , m_layerAnimationDelegate(0)
57 , m_layerScrollClient(0)
61 m_layerId
= s_nextLayerId
++;
64 m_layerAnimationController
= LayerAnimationController::create(m_layerId
);
65 m_layerAnimationController
->addObserver(this);
66 addLayerAnimationEventObserver(m_layerAnimationController
.get());
71 // Our parent should be holding a reference to us so there should be no
72 // way for us to be destroyed while we still have a parent.
75 m_layerAnimationController
->removeObserver(this);
77 // Remove the parent reference from all children.
81 void Layer::setLayerTreeHost(LayerTreeHost
* host
)
83 if (m_layerTreeHost
== host
)
86 m_layerTreeHost
= host
;
88 for (size_t i
= 0; i
< m_children
.size(); ++i
)
89 m_children
[i
]->setLayerTreeHost(host
);
92 m_maskLayer
->setLayerTreeHost(host
);
94 m_replicaLayer
->setLayerTreeHost(host
);
96 m_layerAnimationController
->setAnimationRegistrar(host
? host
->animationRegistrar() : 0);
98 if (host
&& m_layerAnimationController
->hasAnyAnimation())
99 host
->setNeedsCommit();
102 void Layer::setNeedsCommit()
104 if (m_ignoreSetNeedsCommit
)
107 m_layerTreeHost
->setNeedsCommit();
110 void Layer::setNeedsFullTreeSync()
113 m_layerTreeHost
->setNeedsFullTreeSync();
116 gfx::Rect
Layer::layerRectToContentRect(const gfx::RectF
& layerRect
) const
118 gfx::RectF contentRect
= gfx::ScaleRect(layerRect
, contentsScaleX(), contentsScaleY());
119 // Intersect with content rect to avoid the extra pixel because for some
120 // values x and y, ceil((x / y) * y) may be x + 1.
121 contentRect
.Intersect(gfx::Rect(gfx::Point(), contentBounds()));
122 return gfx::ToEnclosingRect(contentRect
);
125 bool Layer::blocksPendingCommit() const
130 bool Layer::canClipSelf() const
135 bool Layer::blocksPendingCommitRecursive() const
137 if (blocksPendingCommit())
139 if (maskLayer() && maskLayer()->blocksPendingCommitRecursive())
141 if (replicaLayer() && replicaLayer()->blocksPendingCommitRecursive())
143 for (size_t i
= 0; i
< m_children
.size(); ++i
)
145 if (m_children
[i
]->blocksPendingCommitRecursive())
151 void Layer::setParent(Layer
* layer
)
153 DCHECK(!layer
|| !layer
->hasAncestor(this));
155 setLayerTreeHost(m_parent
? m_parent
->layerTreeHost() : 0);
157 forceAutomaticRasterScaleToBeRecomputed();
160 bool Layer::hasAncestor(Layer
* ancestor
) const
162 for (const Layer
* layer
= parent(); layer
; layer
= layer
->parent()) {
163 if (layer
== ancestor
)
169 void Layer::addChild(scoped_refptr
<Layer
> child
)
171 insertChild(child
, numChildren());
174 void Layer::insertChild(scoped_refptr
<Layer
> child
, size_t index
)
176 child
->removeFromParent();
177 child
->setParent(this);
178 child
->m_stackingOrderChanged
= true;
180 index
= std::min(index
, m_children
.size());
181 LayerList::iterator iter
= m_children
.begin();
182 m_children
.insert(iter
+ index
, child
);
183 setNeedsFullTreeSync();
186 void Layer::removeFromParent()
189 m_parent
->removeChild(this);
192 void Layer::removeChild(Layer
* child
)
194 for (LayerList::iterator iter
= m_children
.begin(); iter
!= m_children
.end(); ++iter
)
200 m_children
.erase(iter
);
201 setNeedsFullTreeSync();
206 void Layer::replaceChild(Layer
* reference
, scoped_refptr
<Layer
> newLayer
)
209 DCHECK_EQ(reference
->parent(), this);
211 if (reference
== newLayer
)
214 int referenceIndex
= indexOfChild(reference
);
215 if (referenceIndex
== -1) {
220 reference
->removeFromParent();
223 newLayer
->removeFromParent();
224 insertChild(newLayer
, referenceIndex
);
228 int Layer::indexOfChild(const Layer
* reference
)
230 for (size_t i
= 0; i
< m_children
.size(); i
++) {
231 if (m_children
[i
] == reference
)
237 void Layer::setBounds(const gfx::Size
& size
)
239 if (bounds() == size
)
242 bool firstResize
= bounds().IsEmpty() && !size
.IsEmpty();
254 void Layer::didUpdateBounds()
256 m_drawProperties
.content_bounds
= bounds();
259 Layer
* Layer::rootLayer()
262 while (layer
->parent())
263 layer
= layer
->parent();
267 void Layer::removeAllChildren()
269 while (m_children
.size()) {
270 Layer
* layer
= m_children
[0].get();
271 DCHECK(layer
->parent());
272 layer
->removeFromParent();
276 void Layer::setChildren(const LayerList
& children
)
278 if (children
== m_children
)
282 size_t listSize
= children
.size();
283 for (size_t i
= 0; i
< listSize
; i
++)
284 addChild(children
[i
]);
287 Layer
* Layer::childAt(size_t index
)
289 DCHECK_LT(index
, m_children
.size());
290 return m_children
[index
].get();
293 void Layer::setAnchorPoint(const gfx::PointF
& anchorPoint
)
295 if (m_anchorPoint
== anchorPoint
)
297 m_anchorPoint
= anchorPoint
;
301 void Layer::setAnchorPointZ(float anchorPointZ
)
303 if (m_anchorPointZ
== anchorPointZ
)
305 m_anchorPointZ
= anchorPointZ
;
309 void Layer::setBackgroundColor(SkColor backgroundColor
)
311 if (m_backgroundColor
== backgroundColor
)
313 m_backgroundColor
= backgroundColor
;
317 void Layer::calculateContentsScale(
318 float idealContentsScale
,
319 float* contentsScaleX
,
320 float* contentsScaleY
,
321 gfx::Size
* contentBounds
)
325 *contentBounds
= bounds();
328 void Layer::setMasksToBounds(bool masksToBounds
)
330 if (m_masksToBounds
== masksToBounds
)
332 m_masksToBounds
= masksToBounds
;
336 void Layer::setMaskLayer(Layer
* maskLayer
)
338 if (m_maskLayer
== maskLayer
)
341 m_maskLayer
->setLayerTreeHost(0);
342 m_maskLayer
= maskLayer
;
344 m_maskLayer
->setLayerTreeHost(m_layerTreeHost
);
345 m_maskLayer
->setIsMask(true);
347 setNeedsFullTreeSync();
350 void Layer::setReplicaLayer(Layer
* layer
)
352 if (m_replicaLayer
== layer
)
355 m_replicaLayer
->setLayerTreeHost(0);
356 m_replicaLayer
= layer
;
358 m_replicaLayer
->setLayerTreeHost(m_layerTreeHost
);
359 setNeedsFullTreeSync();
362 void Layer::setFilters(const WebKit::WebFilterOperations
& filters
)
364 if (m_filters
== filters
)
369 if (!filters
.isEmpty())
370 LayerTreeHost::setNeedsFilterContext(true);
373 void Layer::setFilter(const skia::RefPtr
<SkImageFilter
>& filter
)
375 if (m_filter
.get() == filter
.get())
377 DCHECK(m_filters
.isEmpty());
381 LayerTreeHost::setNeedsFilterContext(true);
384 void Layer::setBackgroundFilters(const WebKit::WebFilterOperations
& backgroundFilters
)
386 if (m_backgroundFilters
== backgroundFilters
)
388 m_backgroundFilters
= backgroundFilters
;
390 if (!backgroundFilters
.isEmpty())
391 LayerTreeHost::setNeedsFilterContext(true);
394 bool Layer::needsDisplay() const
396 return m_needsDisplay
;
399 void Layer::setOpacity(float opacity
)
401 if (m_opacity
== opacity
)
407 float Layer::opacity() const
412 bool Layer::opacityIsAnimating() const
414 return m_layerAnimationController
->isAnimatingProperty(Animation::Opacity
);
417 void Layer::setContentsOpaque(bool opaque
)
419 if (m_contentsOpaque
== opaque
)
421 m_contentsOpaque
= opaque
;
425 void Layer::setPosition(const gfx::PointF
& position
)
427 if (m_position
== position
)
429 m_position
= position
;
433 void Layer::setSublayerTransform(const gfx::Transform
& sublayerTransform
)
435 if (m_sublayerTransform
== sublayerTransform
)
437 m_sublayerTransform
= sublayerTransform
;
441 void Layer::setTransform(const gfx::Transform
& transform
)
443 if (m_transform
== transform
)
445 m_transform
= transform
;
449 const gfx::Transform
& Layer::transform() const
454 bool Layer::transformIsAnimating() const
456 return m_layerAnimationController
->isAnimatingProperty(Animation::Transform
);
459 void Layer::setScrollOffset(gfx::Vector2d scrollOffset
)
461 if (m_scrollOffset
== scrollOffset
)
463 m_scrollOffset
= scrollOffset
;
464 if (m_layerScrollClient
)
465 m_layerScrollClient
->didScroll();
469 void Layer::setMaxScrollOffset(gfx::Vector2d maxScrollOffset
)
471 if (m_maxScrollOffset
== maxScrollOffset
)
473 m_maxScrollOffset
= maxScrollOffset
;
477 void Layer::setScrollable(bool scrollable
)
479 if (m_scrollable
== scrollable
)
481 m_scrollable
= scrollable
;
485 void Layer::setShouldScrollOnMainThread(bool shouldScrollOnMainThread
)
487 if (m_shouldScrollOnMainThread
== shouldScrollOnMainThread
)
489 m_shouldScrollOnMainThread
= shouldScrollOnMainThread
;
493 void Layer::setHaveWheelEventHandlers(bool haveWheelEventHandlers
)
495 if (m_haveWheelEventHandlers
== haveWheelEventHandlers
)
497 m_haveWheelEventHandlers
= haveWheelEventHandlers
;
501 void Layer::setNonFastScrollableRegion(const Region
& region
)
503 if (m_nonFastScrollableRegion
== region
)
505 m_nonFastScrollableRegion
= region
;
509 void Layer::setTouchEventHandlerRegion(const Region
& region
)
511 if (m_touchEventHandlerRegion
== region
)
513 m_touchEventHandlerRegion
= region
;
516 void Layer::setDrawCheckerboardForMissingTiles(bool checkerboard
)
518 if (m_drawCheckerboardForMissingTiles
== checkerboard
)
520 m_drawCheckerboardForMissingTiles
= checkerboard
;
524 void Layer::setForceRenderSurface(bool force
)
526 if (m_forceRenderSurface
== force
)
528 m_forceRenderSurface
= force
;
532 void Layer::setImplTransform(const gfx::Transform
& transform
)
534 if (m_implTransform
== transform
)
536 m_implTransform
= transform
;
540 void Layer::setDoubleSided(bool doubleSided
)
542 if (m_doubleSided
== doubleSided
)
544 m_doubleSided
= doubleSided
;
548 void Layer::setIsDrawable(bool isDrawable
)
550 if (m_isDrawable
== isDrawable
)
553 m_isDrawable
= isDrawable
;
557 void Layer::setNeedsDisplayRect(const gfx::RectF
& dirtyRect
)
559 m_updateRect
.Union(dirtyRect
);
561 // Simply mark the contents as dirty. For non-root layers, the call to
562 // setNeedsCommit will schedule a fresh compositing pass.
563 // For the root layer, setNeedsCommit has no effect.
564 if (!dirtyRect
.IsEmpty())
565 m_needsDisplay
= true;
571 bool Layer::descendantIsFixedToContainerLayer() const
573 for (size_t i
= 0; i
< m_children
.size(); ++i
) {
574 if (m_children
[i
]->fixedToContainerLayer() || m_children
[i
]->descendantIsFixedToContainerLayer())
580 void Layer::setIsContainerForFixedPositionLayers(bool isContainerForFixedPositionLayers
)
582 if (m_isContainerForFixedPositionLayers
== isContainerForFixedPositionLayers
)
584 m_isContainerForFixedPositionLayers
= isContainerForFixedPositionLayers
;
586 if (m_layerTreeHost
&& m_layerTreeHost
->commitRequested())
589 // Only request a commit if we have a fixed positioned descendant.
590 if (descendantIsFixedToContainerLayer())
594 void Layer::setFixedToContainerLayer(bool fixedToContainerLayer
)
596 if (m_fixedToContainerLayer
== fixedToContainerLayer
)
598 m_fixedToContainerLayer
= fixedToContainerLayer
;
602 void Layer::pushPropertiesTo(LayerImpl
* layer
)
604 layer
->setAnchorPoint(m_anchorPoint
);
605 layer
->setAnchorPointZ(m_anchorPointZ
);
606 layer
->setBackgroundColor(m_backgroundColor
);
607 layer
->setBounds(m_bounds
);
608 layer
->setContentBounds(contentBounds());
609 layer
->setContentsScale(contentsScaleX(), contentsScaleY());
610 layer
->setDebugName(m_debugName
);
611 layer
->setDoubleSided(m_doubleSided
);
612 layer
->setDrawCheckerboardForMissingTiles(m_drawCheckerboardForMissingTiles
);
613 layer
->setForceRenderSurface(m_forceRenderSurface
);
614 layer
->setDrawsContent(drawsContent());
615 layer
->setFilters(filters());
616 layer
->setFilter(filter());
617 layer
->setBackgroundFilters(backgroundFilters());
618 layer
->setMasksToBounds(m_masksToBounds
);
619 layer
->setShouldScrollOnMainThread(m_shouldScrollOnMainThread
);
620 layer
->setHaveWheelEventHandlers(m_haveWheelEventHandlers
);
621 layer
->setNonFastScrollableRegion(m_nonFastScrollableRegion
);
622 layer
->setTouchEventHandlerRegion(m_touchEventHandlerRegion
);
623 layer
->setContentsOpaque(m_contentsOpaque
);
624 if (!opacityIsAnimating())
625 layer
->setOpacity(m_opacity
);
626 layer
->setPosition(m_position
);
627 layer
->setIsContainerForFixedPositionLayers(m_isContainerForFixedPositionLayers
);
628 layer
->setFixedToContainerLayer(m_fixedToContainerLayer
);
629 layer
->setPreserves3D(preserves3D());
630 layer
->setUseParentBackfaceVisibility(m_useParentBackfaceVisibility
);
631 layer
->setSublayerTransform(m_sublayerTransform
);
632 if (!transformIsAnimating())
633 layer
->setTransform(m_transform
);
635 layer
->setScrollable(m_scrollable
);
636 layer
->setScrollOffset(m_scrollOffset
);
637 layer
->setMaxScrollOffset(m_maxScrollOffset
);
639 // If the main thread commits multiple times before the impl thread actually draws, then damage tracking
640 // will become incorrect if we simply clobber the updateRect here. The LayerImpl's updateRect needs to
641 // accumulate (i.e. union) any update changes that have occurred on the main thread.
642 m_updateRect
.Union(layer
->updateRect());
643 layer
->setUpdateRect(m_updateRect
);
645 if (layer
->layerTreeImpl()->settings().implSidePainting
) {
646 DCHECK(layer
->layerTreeImpl()->IsPendingTree());
647 LayerImpl
* active_twin
= layer
->layerTreeImpl()->FindActiveTreeLayerById(id());
648 // Update the scroll delta from the active layer, which may have
649 // adjusted its scroll delta prior to this pending layer being created.
650 // This code is identical to that in LayerImpl::setScrollDelta.
652 DCHECK(layer
->sentScrollDelta().IsZero());
653 layer
->setScrollDelta(active_twin
->scrollDelta() - active_twin
->sentScrollDelta());
656 layer
->setScrollDelta(layer
->scrollDelta() - layer
->sentScrollDelta());
657 layer
->setSentScrollDelta(gfx::Vector2d());
660 layer
->setStackingOrderChanged(m_stackingOrderChanged
);
662 m_layerAnimationController
->pushAnimationUpdatesTo(layer
->layerAnimationController());
664 // Reset any state that should be cleared for the next update.
665 m_stackingOrderChanged
= false;
666 m_updateRect
= gfx::RectF();
669 scoped_ptr
<LayerImpl
> Layer::createLayerImpl(LayerTreeImpl
* treeImpl
)
671 return LayerImpl::create(treeImpl
, m_layerId
);
674 bool Layer::drawsContent() const
679 bool Layer::needMoreUpdates()
684 void Layer::setDebugName(const std::string
& debugName
)
686 m_debugName
= debugName
;
690 void Layer::setRasterScale(float scale
)
692 if (m_rasterScale
== scale
)
694 m_rasterScale
= scale
;
696 // When automatically computed, this acts like a draw property.
697 if (m_automaticallyComputeRasterScale
)
702 void Layer::setAutomaticallyComputeRasterScale(bool automatic
)
704 if (m_automaticallyComputeRasterScale
== automatic
)
706 m_automaticallyComputeRasterScale
= automatic
;
708 if (m_automaticallyComputeRasterScale
)
709 forceAutomaticRasterScaleToBeRecomputed();
714 void Layer::forceAutomaticRasterScaleToBeRecomputed()
716 if (!m_automaticallyComputeRasterScale
)
722 void Layer::setBoundsContainPageScale(bool boundsContainPageScale
)
724 for (size_t i
= 0; i
< m_children
.size(); ++i
)
725 m_children
[i
]->setBoundsContainPageScale(boundsContainPageScale
);
727 if (boundsContainPageScale
== m_boundsContainPageScale
)
730 m_boundsContainPageScale
= boundsContainPageScale
;
734 void Layer::createRenderSurface()
736 DCHECK(!m_drawProperties
.render_surface
);
737 m_drawProperties
.render_surface
= make_scoped_ptr(new RenderSurface(this));
738 m_drawProperties
.render_target
= this;
741 int Layer::id() const
746 void Layer::OnOpacityAnimated(float opacity
)
748 // This is called due to an ongoing accelerated animation. Since this animation is
749 // also being run on the impl thread, there is no need to request a commit to push
750 // this value over, so set the value directly rather than calling setOpacity.
754 void Layer::OnTransformAnimated(const gfx::Transform
& transform
)
756 // This is called due to an ongoing accelerated animation. Since this animation is
757 // also being run on the impl thread, there is no need to request a commit to push
758 // this value over, so set this value directly rather than calling setTransform.
759 m_transform
= transform
;
762 bool Layer::IsActive() const
767 bool Layer::addAnimation(scoped_ptr
<Animation
> animation
)
769 // WebCore currently assumes that accelerated animations will start soon
770 // after the animation is added. However we cannot guarantee that if we do
771 // not have a layerTreeHost that will setNeedsCommit().
772 // Unfortunately, the fix below to guarantee correctness causes performance
773 // regressions on Android, since Android has shipped for a long time
774 // with all animations accelerated. For this reason, we will live with
775 // this bug only on Android until the bug is fixed.
776 // http://crbug.com/129683
777 #if !defined(OS_ANDROID)
778 if (!m_layerTreeHost
)
781 if (!m_layerTreeHost
->settings().acceleratedAnimationEnabled
)
785 m_layerAnimationController
->addAnimation(animation
.Pass());
790 void Layer::pauseAnimation(int animationId
, double timeOffset
)
792 m_layerAnimationController
->pauseAnimation(animationId
, timeOffset
);
796 void Layer::removeAnimation(int animationId
)
798 m_layerAnimationController
->removeAnimation(animationId
);
802 void Layer::suspendAnimations(double monotonicTime
)
804 m_layerAnimationController
->suspendAnimations(monotonicTime
);
808 void Layer::resumeAnimations(double monotonicTime
)
810 m_layerAnimationController
->resumeAnimations(monotonicTime
);
814 void Layer::setLayerAnimationController(scoped_refptr
<LayerAnimationController
> layerAnimationController
)
816 removeLayerAnimationEventObserver(m_layerAnimationController
.get());
817 m_layerAnimationController
->removeObserver(this);
818 m_layerAnimationController
= layerAnimationController
;
819 m_layerAnimationController
->setForceSync();
820 m_layerAnimationController
->addObserver(this);
821 addLayerAnimationEventObserver(m_layerAnimationController
.get());
825 scoped_refptr
<LayerAnimationController
> Layer::releaseLayerAnimationController()
827 m_layerAnimationController
->removeObserver(this);
828 scoped_refptr
<LayerAnimationController
> toReturn
= m_layerAnimationController
;
829 m_layerAnimationController
= LayerAnimationController::create(id());
830 m_layerAnimationController
->addObserver(this);
834 bool Layer::hasActiveAnimation() const
836 return m_layerAnimationController
->hasActiveAnimation();
839 void Layer::notifyAnimationStarted(const AnimationEvent
& event
, double wallClockTime
)
841 FOR_EACH_OBSERVER(LayerAnimationEventObserver
, m_layerAnimationObservers
,
842 OnAnimationStarted(event
));
843 if (m_layerAnimationDelegate
)
844 m_layerAnimationDelegate
->notifyAnimationStarted(wallClockTime
);
847 void Layer::notifyAnimationFinished(double wallClockTime
)
849 if (m_layerAnimationDelegate
)
850 m_layerAnimationDelegate
->notifyAnimationFinished(wallClockTime
);
853 void Layer::addLayerAnimationEventObserver(LayerAnimationEventObserver
* animationObserver
)
855 if (!m_layerAnimationObservers
.HasObserver(animationObserver
))
856 m_layerAnimationObservers
.AddObserver(animationObserver
);
859 void Layer::removeLayerAnimationEventObserver(LayerAnimationEventObserver
* animationObserver
)
861 m_layerAnimationObservers
.RemoveObserver(animationObserver
);
864 Region
Layer::visibleContentOpaqueRegion() const
866 if (contentsOpaque())
867 return visibleContentRect();
871 ScrollbarLayer
* Layer::toScrollbarLayer()
876 void sortLayers(std::vector
<scoped_refptr
<Layer
> >::iterator
, std::vector
<scoped_refptr
<Layer
> >::iterator
, void*)
878 // Currently we don't use z-order to decide what to paint, so there's no need to actually sort Layers.