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 "AnimationControllerPrivate.h" 32 #include "CompositeAnimation.h" 33 #include "CSSPropertyNames.h" 34 #include "EventNames.h" 35 #include "ImplicitAnimation.h" 36 #include "KeyframeAnimation.h" 37 #include "RenderLayer.h" 38 #include "RenderLayerBacking.h" 39 #include <wtf/UnusedParam.h> 40 41 namespace WebCore { 42 43 ImplicitAnimation::ImplicitAnimation(const Animation* transition, int 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 != cAnimateAll); 52 } 53 54 ImplicitAnimation::~ImplicitAnimation() 55 { 56 // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed. 57 if (!postActive()) 58 endAnimation(); 59 } 60 61 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const 62 { 63 return m_object->document()->hasListenerType(inListenerType); 64 } 65 66 void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle) 67 { 68 // If we get this far and the animation is done, it means we are cleaning up a just finished animation. 69 // So just return. Everything is already all cleaned up. 70 if (postActive()) 71 return; 72 73 // Reset to start the transition if we are new 74 if (isNew()) 75 reset(targetStyle); 76 77 // Run a cycle of animation. 78 // We know we will need a new render style, so make one if needed 79 if (!animatedStyle) 80 animatedStyle = RenderStyle::clone(targetStyle); 81 82 bool needsAnim = blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); 83 // FIXME: we also need to detect cases where we have to software animate for other reasons, 84 // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902 85 if (needsAnim) 86 setAnimating(); 87 else { 88 #if USE(ACCELERATED_COMPOSITING) 89 // If we are running an accelerated animation, set a flag in the style which causes the style 90 // to compare as different to any other style. This ensures that changes to the property 91 // that is animating are correctly detected during the animation (e.g. when a transition 92 // gets interrupted). 93 animatedStyle->setIsRunningAcceleratedAnimation(); 94 #endif 95 } 96 97 // Fire the start timeout if needed 98 fireAnimationEventsIfNeeded(); 99 } 100 101 void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle) 102 { 103 if (!animatedStyle) 104 animatedStyle = RenderStyle::clone(m_toStyle.get()); 105 106 blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); 107 } 108 109 bool ImplicitAnimation::startAnimation(double timeOffset) 110 { 111 #if USE(ACCELERATED_COMPOSITING) 112 if (m_object && m_object->hasLayer()) { 113 RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); 114 if (layer->isComposited()) 115 return layer->backing()->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get()); 116 } 117 #else 118 UNUSED_PARAM(timeOffset); 119 #endif 120 return false; 121 } 122 123 void ImplicitAnimation::pauseAnimation(double timeOffset) 124 { 125 if (!m_object) 126 return; 127 128 #if USE(ACCELERATED_COMPOSITING) 129 if (m_object->hasLayer()) { 130 RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); 131 if (layer->isComposited()) 132 layer->backing()->transitionPaused(timeOffset, m_animatingProperty); 133 } 134 #else 135 UNUSED_PARAM(timeOffset); 136 #endif 137 // Restore the original (unanimated) style 138 if (!paused()) 139 setNeedsStyleRecalc(m_object->node()); 140 } 141 142 void ImplicitAnimation::endAnimation() 143 { 144 #if USE(ACCELERATED_COMPOSITING) 145 if (m_object && m_object->hasLayer()) { 146 RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); 147 if (layer->isComposited()) 148 layer->backing()->transitionFinished(m_animatingProperty); 149 } 150 #endif 151 } 152 153 void ImplicitAnimation::onAnimationEnd(double elapsedTime) 154 { 155 // If we have a keyframe animation on this property, this transition is being overridden. The keyframe 156 // animation keeps an unanimated style in case a transition starts while the keyframe animation is 157 // running. But now that the transition has completed, we need to update this style with its new 158 // destination. If we didn't, the next time through we would think a transition had started 159 // (comparing the old unanimated style with the new final style of the transition). 160 RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty); 161 if (keyframeAnim) 162 keyframeAnim->setUnanimatedStyle(m_toStyle); 163 164 sendTransitionEvent(eventNames().webkitTransitionEndEvent, elapsedTime); 165 endAnimation(); 166 } 167 168 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime) 169 { 170 if (eventType == eventNames().webkitTransitionEndEvent) { 171 Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER; 172 173 if (shouldSendEventForListener(listenerType)) { 174 String propertyName; 175 if (m_animatingProperty != cAnimateAll) 176 propertyName = getPropertyName(static_cast<CSSPropertyID>(m_animatingProperty)); 177 178 // Dispatch the event 179 RefPtr<Element> element = 0; 180 if (m_object->node() && m_object->node()->isElementNode()) 181 element = static_cast<Element*>(m_object->node()); 182 183 ASSERT(!element || (element->document() && !element->document()->inPageCache())); 184 if (!element) 185 return false; 186 187 // Schedule event handling 188 m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime); 189 190 // Restore the original (unanimated) style 191 if (eventType == eventNames().webkitTransitionEndEvent && element->renderer()) 192 setNeedsStyleRecalc(element.get()); 193 194 return true; // Did dispatch an event 195 } 196 } 197 198 return false; // Didn't dispatch an event 199 } 200 201 void ImplicitAnimation::reset(RenderStyle* to) 202 { 203 ASSERT(to); 204 ASSERT(m_fromStyle); 205 206 m_toStyle = to; 207 208 // Restart the transition 209 if (m_fromStyle && m_toStyle) 210 updateStateMachine(AnimationStateInputRestartAnimation, -1); 211 212 // set the transform animation list 213 validateTransformFunctionList(); 214 } 215 216 void ImplicitAnimation::setOverridden(bool b) 217 { 218 if (b == m_overridden) 219 return; 220 221 m_overridden = b; 222 updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1); 223 } 224 225 bool ImplicitAnimation::affectsProperty(int property) const 226 { 227 return (m_animatingProperty == property); 228 } 229 230 bool ImplicitAnimation::isTargetPropertyEqual(int prop, const RenderStyle* targetStyle) 231 { 232 // We can get here for a transition that has not started yet. This would make m_toStyle unset and null. 233 // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>) 234 if (!m_toStyle) 235 return false; 236 return propertiesEqual(prop, m_toStyle.get(), targetStyle); 237 } 238 239 void ImplicitAnimation::blendPropertyValueInStyle(int prop, RenderStyle* currentStyle) 240 { 241 // We should never add a transition with a 0 duration and delay. But if we ever did 242 // it would have a null toStyle. So just in case, let's check that here. (See 243 // <https://bugs.webkit.org/show_bug.cgi?id=24787> 244 if (!m_toStyle) 245 return; 246 247 blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); 248 } 249 250 void ImplicitAnimation::validateTransformFunctionList() 251 { 252 m_transformFunctionListValid = false; 253 254 if (!m_fromStyle || !m_toStyle) 255 return; 256 257 const TransformOperations* val = &m_fromStyle->transform(); 258 const TransformOperations* toVal = &m_toStyle->transform(); 259 260 if (val->operations().isEmpty()) 261 val = toVal; 262 263 if (val->operations().isEmpty()) 264 return; 265 266 // See if the keyframes are valid 267 if (val != toVal) { 268 // A list of length 0 matches anything 269 if (!toVal->operations().isEmpty()) { 270 // If the sizes of the function lists don't match, the lists don't match 271 if (val->operations().size() != toVal->operations().size()) 272 return; 273 274 // If the types of each function are not the same, the lists don't match 275 for (size_t j = 0; j < val->operations().size(); ++j) { 276 if (!val->operations()[j]->isSameType(*toVal->operations()[j])) 277 return; 278 } 279 } 280 } 281 282 // Keyframes are valid 283 m_transformFunctionListValid = true; 284 } 285 286 double ImplicitAnimation::timeToNextService() 287 { 288 double t = AnimationBase::timeToNextService(); 289 #if USE(ACCELERATED_COMPOSITING) 290 if (t != 0 || preActive()) 291 return t; 292 293 // A return value of 0 means we need service. But if this is an accelerated animation we 294 // only need service at the end of the transition. 295 if (animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) { 296 bool isLooping; 297 getTimeToNextEvent(t, isLooping); 298 } 299 #endif 300 return t; 301 } 302 303 } // namespace WebCore 304