Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2007 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 
     31 #include "core/dom/EventNames.h"
     32 #include "core/page/UseCounter.h"
     33 #include "core/page/animation/AnimationControllerPrivate.h"
     34 #include "core/page/animation/CSSPropertyAnimation.h"
     35 #include "core/page/animation/CompositeAnimation.h"
     36 #include "core/page/animation/ImplicitAnimation.h"
     37 #include "core/page/animation/KeyframeAnimation.h"
     38 #include "core/rendering/RenderBoxModelObject.h"
     39 #include "public/platform/Platform.h"
     40 
     41 namespace WebCore {
     42 
     43 ImplicitAnimation::ImplicitAnimation(const CSSAnimationData* transition, CSSPropertyID animatingProperty, RenderObject* renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle)
     44     : AnimationBase(transition, renderer, compAnim)
     45     , m_transitionProperty(transition->property())
     46     , m_animatingProperty(animatingProperty)
     47     , m_overridden(false)
     48     , m_active(true)
     49     , m_fromStyle(fromStyle)
     50 {
     51     ASSERT(animatingProperty != CSSPropertyInvalid);
     52     WebKit::Platform::current()->histogramSparse("WebCore.Animation.CSSProperties", UseCounter::mapCSSPropertyIdToCSSSampleIdForHistogram(m_animatingProperty));
     53 }
     54 
     55 ImplicitAnimation::~ImplicitAnimation()
     56 {
     57     // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed.
     58     if (!postActive())
     59         endAnimation();
     60 }
     61 
     62 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const
     63 {
     64     return m_object->document()->hasListenerType(inListenerType);
     65 }
     66 
     67 void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
     68 {
     69     // If we get this far and the animation is done, it means we are cleaning up a just finished animation.
     70     // So just return. Everything is already all cleaned up.
     71     if (postActive())
     72         return;
     73 
     74     // Reset to start the transition if we are new
     75     if (isNew())
     76         reset(targetStyle);
     77 
     78     // Run a cycle of animation.
     79     // We know we will need a new render style, so make one if needed
     80     if (!animatedStyle)
     81         animatedStyle = RenderStyle::clone(targetStyle);
     82 
     83     bool needsAnim = CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
     84     // FIXME: we also need to detect cases where we have to software animate for other reasons,
     85     // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902
     86     if (!needsAnim)
     87         // If we are running an accelerated animation, set a flag in the style which causes the style
     88         // to compare as different to any other style. This ensures that changes to the property
     89         // that is animating are correctly detected during the animation (e.g. when a transition
     90         // gets interrupted).
     91         animatedStyle->setIsRunningAcceleratedAnimation();
     92 
     93     // Fire the start timeout if needed
     94     fireAnimationEventsIfNeeded();
     95 }
     96 
     97 void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
     98 {
     99     if (!animatedStyle)
    100         animatedStyle = RenderStyle::clone(m_toStyle.get());
    101 
    102     CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
    103 }
    104 
    105 bool ImplicitAnimation::startAnimation(double timeOffset)
    106 {
    107     if (m_object && m_object->isComposited())
    108         return toRenderBoxModelObject(m_object)->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
    109 
    110     return false;
    111 }
    112 
    113 void ImplicitAnimation::pauseAnimation(double timeOffset)
    114 {
    115     if (!m_object)
    116         return;
    117 
    118     if (m_object->isComposited())
    119         toRenderBoxModelObject(m_object)->transitionPaused(timeOffset, m_animatingProperty);
    120 
    121     // Restore the original (unanimated) style
    122     if (!paused())
    123         setNeedsStyleRecalc(m_object->node());
    124 }
    125 
    126 void ImplicitAnimation::endAnimation()
    127 {
    128     if (m_object && m_object->isComposited())
    129         toRenderBoxModelObject(m_object)->transitionFinished(m_animatingProperty);
    130 }
    131 
    132 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
    133 {
    134     // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
    135     // animation keeps an unanimated style in case a transition starts while the keyframe animation is
    136     // running. But now that the transition has completed, we need to update this style with its new
    137     // destination. If we didn't, the next time through we would think a transition had started
    138     // (comparing the old unanimated style with the new final style of the transition).
    139     RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty);
    140     if (keyframeAnim)
    141         keyframeAnim->setUnanimatedStyle(m_toStyle);
    142 
    143     sendTransitionEvent(eventNames().transitionendEvent, elapsedTime);
    144     endAnimation();
    145 }
    146 
    147 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
    148 {
    149     if (eventType == eventNames().transitionendEvent) {
    150         Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
    151 
    152         if (shouldSendEventForListener(listenerType)) {
    153             String propertyName = getPropertyNameString(m_animatingProperty);
    154 
    155             // Dispatch the event
    156             RefPtr<Element> element = 0;
    157             if (m_object->node() && m_object->node()->isElementNode())
    158                 element = toElement(m_object->node());
    159 
    160             if (!element)
    161                 return false;
    162 
    163             // Schedule event handling
    164             m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime);
    165 
    166             // Restore the original (unanimated) style
    167             if (eventType == eventNames().transitionendEvent && element->renderer())
    168                 setNeedsStyleRecalc(element.get());
    169 
    170             return true; // Did dispatch an event
    171         }
    172     }
    173 
    174     return false; // Didn't dispatch an event
    175 }
    176 
    177 void ImplicitAnimation::reset(RenderStyle* to)
    178 {
    179     ASSERT(to);
    180     ASSERT(m_fromStyle);
    181 
    182     m_toStyle = to;
    183 
    184     // Restart the transition
    185     if (m_fromStyle && m_toStyle)
    186         updateStateMachine(AnimationStateInputRestartAnimation, -1);
    187 
    188     // set the transform animation list
    189     validateTransformFunctionList();
    190     checkForMatchingFilterFunctionLists();
    191 }
    192 
    193 void ImplicitAnimation::setOverridden(bool b)
    194 {
    195     if (b == m_overridden)
    196         return;
    197 
    198     m_overridden = b;
    199     updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1);
    200 }
    201 
    202 bool ImplicitAnimation::affectsProperty(CSSPropertyID property) const
    203 {
    204     return (m_animatingProperty == property);
    205 }
    206 
    207 bool ImplicitAnimation::isTargetPropertyEqual(CSSPropertyID prop, const RenderStyle* targetStyle)
    208 {
    209     // We can get here for a transition that has not started yet. This would make m_toStyle unset and null.
    210     // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>)
    211     if (!m_toStyle)
    212         return false;
    213     return CSSPropertyAnimation::propertiesEqual(prop, m_toStyle.get(), targetStyle);
    214 }
    215 
    216 void ImplicitAnimation::blendPropertyValueInStyle(CSSPropertyID prop, RenderStyle* currentStyle)
    217 {
    218     // We should never add a transition with a 0 duration and delay. But if we ever did
    219     // it would have a null toStyle. So just in case, let's check that here. (See
    220     // <https://bugs.webkit.org/show_bug.cgi?id=24787>
    221     if (!m_toStyle)
    222         return;
    223 
    224     CSSPropertyAnimation::blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
    225 }
    226 
    227 void ImplicitAnimation::validateTransformFunctionList()
    228 {
    229     m_transformFunctionListValid = false;
    230 
    231     if (!m_fromStyle || !m_toStyle)
    232         return;
    233 
    234     const TransformOperations* val = &m_fromStyle->transform();
    235     const TransformOperations* toVal = &m_toStyle->transform();
    236 
    237     if (val->operations().isEmpty())
    238         val = toVal;
    239 
    240     if (val->operations().isEmpty())
    241         return;
    242 
    243     // An emtpy transform list matches anything.
    244     if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
    245         return;
    246 
    247     // Transform lists match.
    248     m_transformFunctionListValid = true;
    249 }
    250 
    251 void ImplicitAnimation::checkForMatchingFilterFunctionLists()
    252 {
    253     m_filterFunctionListsMatch = false;
    254 
    255     if (!m_fromStyle || !m_toStyle)
    256         return;
    257 
    258     const FilterOperations* val = &m_fromStyle->filter();
    259     const FilterOperations* toVal = &m_toStyle->filter();
    260 
    261     if (val->operations().isEmpty())
    262         val = toVal;
    263 
    264     if (val->operations().isEmpty())
    265         return;
    266 
    267     // An emtpy filter list matches anything.
    268     if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
    269         return;
    270 
    271     // Filter lists match.
    272     m_filterFunctionListsMatch = true;
    273 }
    274 
    275 double ImplicitAnimation::timeToNextService()
    276 {
    277     double t = AnimationBase::timeToNextService();
    278     if (t != 0 || preActive())
    279         return t;
    280 
    281     // A return value of 0 means we need service. But if this is an accelerated animation we
    282     // only need service at the end of the transition.
    283     if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) {
    284         bool isLooping;
    285         getTimeToNextEvent(t, isLooping);
    286     }
    287 
    288     return t;
    289 }
    290 
    291 } // namespace WebCore
    292