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/events/ThreadLocalEventNames.h"
     32 #include "core/frame/UseCounter.h"
     33 #include "core/frame/animation/AnimationControllerPrivate.h"
     34 #include "core/frame/animation/CSSPropertyAnimation.h"
     35 #include "core/frame/animation/CompositeAnimation.h"
     36 #include "core/frame/animation/ImplicitAnimation.h"
     37 #include "core/frame/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     blink::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 void ImplicitAnimation::startAnimation(double timeOffset)
    106 {
    107     if (m_object && m_object->compositingState() == PaintsIntoOwnBacking)
    108         m_isAccelerated = toRenderBoxModelObject(m_object)->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
    109 }
    110 
    111 void ImplicitAnimation::pauseAnimation(double timeOffset)
    112 {
    113     if (!m_object)
    114         return;
    115 
    116     if (m_object && m_object->compositingState() == PaintsIntoOwnBacking && isAccelerated())
    117         toRenderBoxModelObject(m_object)->transitionPaused(timeOffset, m_animatingProperty);
    118 
    119     // Restore the original (unanimated) style
    120     if (!paused())
    121         setNeedsStyleRecalc(m_object->node());
    122 }
    123 
    124 void ImplicitAnimation::endAnimation()
    125 {
    126     if (m_object && m_object->compositingState() == PaintsIntoOwnBacking && isAccelerated())
    127         toRenderBoxModelObject(m_object)->transitionFinished(m_animatingProperty);
    128     m_isAccelerated = false;
    129 }
    130 
    131 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
    132 {
    133     // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
    134     // animation keeps an unanimated style in case a transition starts while the keyframe animation is
    135     // running. But now that the transition has completed, we need to update this style with its new
    136     // destination. If we didn't, the next time through we would think a transition had started
    137     // (comparing the old unanimated style with the new final style of the transition).
    138     RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty);
    139     if (keyframeAnim)
    140         keyframeAnim->setUnanimatedStyle(m_toStyle);
    141 
    142     sendTransitionEvent(EventTypeNames::transitionend, elapsedTime);
    143     endAnimation();
    144 }
    145 
    146 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
    147 {
    148     if (eventType == EventTypeNames::transitionend) {
    149         Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
    150 
    151         if (shouldSendEventForListener(listenerType)) {
    152             String propertyName = getPropertyNameString(m_animatingProperty);
    153 
    154             // Dispatch the event
    155             RefPtr<Element> element = 0;
    156             if (m_object->node() && m_object->node()->isElementNode())
    157                 element = toElement(m_object->node());
    158 
    159             if (!element)
    160                 return false;
    161 
    162             // Schedule event handling
    163             m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime);
    164 
    165             // Restore the original (unanimated) style
    166             if (eventType == EventTypeNames::transitionend && element->renderer())
    167                 setNeedsStyleRecalc(element.get());
    168 
    169             return true; // Did dispatch an event
    170         }
    171     }
    172 
    173     return false; // Didn't dispatch an event
    174 }
    175 
    176 void ImplicitAnimation::reset(RenderStyle* to)
    177 {
    178     ASSERT(to);
    179     ASSERT(m_fromStyle);
    180 
    181     m_toStyle = to;
    182 
    183     // Restart the transition
    184     if (m_fromStyle && m_toStyle)
    185         updateStateMachine(AnimationStateInputRestartAnimation, -1);
    186 
    187     // set the transform animation list
    188     validateTransformFunctionList();
    189     checkForMatchingFilterFunctionLists();
    190 }
    191 
    192 void ImplicitAnimation::setOverridden(bool b)
    193 {
    194     if (b == m_overridden)
    195         return;
    196 
    197     m_overridden = b;
    198     updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1);
    199 }
    200 
    201 bool ImplicitAnimation::affectsProperty(CSSPropertyID property) const
    202 {
    203     return (m_animatingProperty == property);
    204 }
    205 
    206 bool ImplicitAnimation::isTargetPropertyEqual(CSSPropertyID prop, const RenderStyle* targetStyle)
    207 {
    208     // We can get here for a transition that has not started yet. This would make m_toStyle unset and null.
    209     // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>)
    210     if (!m_toStyle)
    211         return false;
    212     return CSSPropertyAnimation::propertiesEqual(prop, m_toStyle.get(), targetStyle);
    213 }
    214 
    215 void ImplicitAnimation::blendPropertyValueInStyle(CSSPropertyID prop, RenderStyle* currentStyle)
    216 {
    217     // We should never add a transition with a 0 duration and delay. But if we ever did
    218     // it would have a null toStyle. So just in case, let's check that here. (See
    219     // <https://bugs.webkit.org/show_bug.cgi?id=24787>
    220     if (!m_toStyle)
    221         return;
    222 
    223     CSSPropertyAnimation::blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
    224 }
    225 
    226 void ImplicitAnimation::validateTransformFunctionList()
    227 {
    228     m_transformFunctionListValid = false;
    229 
    230     if (!m_fromStyle || !m_toStyle)
    231         return;
    232 
    233     const TransformOperations* val = &m_fromStyle->transform();
    234     const TransformOperations* toVal = &m_toStyle->transform();
    235 
    236     if (val->operations().isEmpty())
    237         val = toVal;
    238 
    239     if (val->operations().isEmpty())
    240         return;
    241 
    242     // An emtpy transform list matches anything.
    243     if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
    244         return;
    245 
    246     // Transform lists match.
    247     m_transformFunctionListValid = true;
    248 }
    249 
    250 void ImplicitAnimation::checkForMatchingFilterFunctionLists()
    251 {
    252     m_filterFunctionListsMatch = false;
    253 
    254     if (!m_fromStyle || !m_toStyle)
    255         return;
    256 
    257     const FilterOperations* val = &m_fromStyle->filter();
    258     const FilterOperations* toVal = &m_toStyle->filter();
    259 
    260     if (!val->canInterpolateWith(*toVal))
    261         return;
    262 
    263     // Filter lists match.
    264     m_filterFunctionListsMatch = true;
    265 }
    266 
    267 double ImplicitAnimation::timeToNextService()
    268 {
    269     double t = AnimationBase::timeToNextService();
    270     if (t != 0 || preActive())
    271         return t;
    272 
    273     // A return value of 0 means we need service. But if this is an accelerated animation we
    274     // only need service at the end of the transition.
    275     if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) {
    276         bool isLooping;
    277         getTimeToNextEvent(t, isLooping);
    278     }
    279 
    280     return t;
    281 }
    282 
    283 } // namespace WebCore
    284