Index: Source/core/animation/css/CSSAnimations.cpp |
diff --git a/Source/core/animation/css/CSSAnimations.cpp b/Source/core/animation/css/CSSAnimations.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b6bfc283b7451d970eaa5541cf2133cd72d786ad |
--- /dev/null |
+++ b/Source/core/animation/css/CSSAnimations.cpp |
@@ -0,0 +1,259 @@ |
+/* |
+ * Copyright (C) 2013 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "config.h" |
+#include "core/animation/css/CSSAnimations.h" |
+ |
+#include "core/animation/DocumentTimeline.h" |
+#include "core/animation/KeyframeAnimationEffect.h" |
+#include "core/css/CSSKeyframeRule.h" |
+#include "core/css/resolver/StyleResolver.h" |
+#include "core/dom/AnimationEvent.h" |
+#include "core/dom/Element.h" |
+#include "core/dom/EventNames.h" |
+#include "core/platform/animation/CSSAnimationDataList.h" |
+#include "core/platform/animation/TimingFunction.h" |
+#include "wtf/HashSet.h" |
+ |
+namespace WebCore { |
+ |
+void timingFromAnimationData(const CSSAnimationData* animationData, Timing& timing) |
+{ |
+ if (animationData->isDelaySet()) |
+ timing.startDelay = animationData->delay(); |
+ if (animationData->isDurationSet()) { |
+ timing.iterationDuration = animationData->duration(); |
+ timing.hasIterationDuration = true; |
+ } |
+ if (animationData->isIterationCountSet()) { |
+ if (animationData->iterationCount() == CSSAnimationData::IterationCountInfinite) |
+ timing.iterationCount = std::numeric_limits<double>::infinity(); |
+ else |
+ timing.iterationCount = animationData->iterationCount(); |
+ } |
+ if (animationData->isTimingFunctionSet()) { |
+ if (!animationData->timingFunction()->isLinearTimingFunction()) |
+ timing.timingFunction = animationData->timingFunction(); |
+ } else { |
+ // CSS default is ease, default in model is linear. |
+ timing.timingFunction = CubicBezierTimingFunction::preset(CubicBezierTimingFunction::Ease); |
+ } |
+ if (animationData->isFillModeSet()) { |
+ switch (animationData->fillMode()) { |
+ case AnimationFillModeForwards: |
+ timing.fillMode = Timing::FillModeForwards; |
+ break; |
+ case AnimationFillModeBackwards: |
+ timing.fillMode = Timing::FillModeBackwards; |
+ break; |
+ case AnimationFillModeBoth: |
+ timing.fillMode = Timing::FillModeBoth; |
+ break; |
+ case AnimationFillModeNone: |
+ timing.fillMode = Timing::FillModeNone; |
+ break; |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ } |
+ } |
+ if (animationData->isDirectionSet()) { |
+ switch (animationData->direction()) { |
+ case CSSAnimationData::AnimationDirectionNormal: |
+ timing.direction = Timing::PlaybackDirectionNormal; |
+ break; |
+ case CSSAnimationData::AnimationDirectionAlternate: |
+ timing.direction = Timing::PlaybackDirectionAlternate; |
+ break; |
+ case CSSAnimationData::AnimationDirectionReverse: |
+ timing.direction = Timing::PlaybackDirectionReverse; |
+ break; |
+ case CSSAnimationData::AnimationDirectionAlternateReverse: |
+ timing.direction = Timing::PlaybackDirectionAlternateReverse; |
+ break; |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ } |
+ } |
+} |
+ |
+void resolveKeyframes(Element* element, const StringImpl* name, StyleResolver* resolver, KeyframeAnimationEffect::KeyframeVector& keyframes) |
+{ |
+ const StyleRuleKeyframes* keyframesRule = resolver->matchScopedKeyframesRule(element, name); |
+ if (!keyframesRule) |
+ return; |
+ |
+ // Construct and populate the style for each keyframe |
+ const Vector<RefPtr<StyleKeyframe> >& styleKeyframes = keyframesRule->keyframes(); |
+ for (unsigned i = 0; i < styleKeyframes.size(); ++i) { |
+ const StyleKeyframe* styleKeyframe = styleKeyframes[i].get(); |
+ |
+ Vector<float> offsets; |
+ styleKeyframe->getKeys(offsets); |
+ for (size_t j = 0; j < offsets.size(); ++j) { |
+ RefPtr<Keyframe> keyframe = Keyframe::create(); |
+ keyframe->setOffset(offsets[j]); |
+ const StylePropertySet* properties = styleKeyframe->properties(); |
+ // FIXME: AnimatableValues should be shared between the keyframes at different offsets. |
+ for (unsigned k = 0; k < properties->propertyCount(); k++) { |
+ CSSPropertyID property = properties->propertyAt(k).id(); |
+ // FIXME: CSSValue needs to be resolved. |
+ keyframe->setPropertyValue(property, AnimatableValue::create(properties->getPropertyCSSValue(property).get()).get()); |
+ } |
+ keyframes.append(keyframe); |
+ } |
+ } |
+ |
+ // FIXME: If the 0% keyframe is missing, create it (but only if there is at least one other keyframe) |
+ // FIXME: If the 100% keyframe is missing, create it (but only if there is at least one other keyframe) |
+} |
+ |
+const StylePropertySet* firstKeyframeStyles(const Element* element, const StringImpl* animationName, StyleResolver* resolver) |
dglazkov
2013/08/01 16:12:46
We should probably move this to StyleResolver, sin
dstockwell
2013/08/02 00:11:01
Done.
|
+{ |
+ const StyleRuleKeyframes* keyframesRule = resolver->matchScopedKeyframesRule(element, animationName); |
+ if (!keyframesRule) |
+ return 0; |
+ |
+ // Find the last keyframe at offset 0 |
+ const StyleKeyframe* firstKeyframe = 0; |
+ const Vector<RefPtr<StyleKeyframe> >& styleKeyframes = keyframesRule->keyframes(); |
+ for (unsigned i = 0; i < styleKeyframes.size(); ++i) { |
+ const StyleKeyframe* styleKeyframe = styleKeyframes[i].get(); |
+ |
+ Vector<float> offsets; |
+ styleKeyframe->getKeys(offsets); |
+ for (size_t j = 0; j < offsets.size(); ++j) { |
+ if (!offsets[j]) { |
+ firstKeyframe = styleKeyframe; |
+ break; |
+ } |
+ } |
+ } |
+ |
+ return firstKeyframe ? firstKeyframe->properties() : 0; |
+} |
+ |
+bool CSSAnimations::needsUpdate(const Element* element, const RenderStyle* style) |
+{ |
+ ActiveAnimations* activeAnimations = element->activeAnimations(); |
+ const CSSAnimationDataList* animations = style->animations(); |
+ const CSSAnimations* cssAnimations = activeAnimations ? activeAnimations->cssAnimations() : 0; |
+ EDisplay display = style->display(); |
+ return (display != NONE && animations && animations->size()) || (cssAnimations && !cssAnimations->isEmpty()); |
+} |
+ |
+PassOwnPtr<CSSAnimationUpdate> CSSAnimations::calculateUpdate(const Element* element, EDisplay display, const CSSAnimations* cssAnimations, const CSSAnimationDataList* animationDataList, StyleResolver* resolver) |
+{ |
+ OwnPtr<CSSAnimationUpdate> update; |
+ HashSet<StringImpl*> inactive; |
+ if (cssAnimations) |
+ for (AnimationMap::const_iterator iter = cssAnimations->m_animations.begin(); iter != cssAnimations->m_animations.end(); ++iter) |
+ inactive.add(iter->key); |
+ |
+ RefPtr<MutableStylePropertySet> newStyles; |
+ if (display != NONE) { |
+ for (int i = 0; animationDataList && i < animationDataList->size(); ++i) { |
+ const CSSAnimationData* animationData = animationDataList->animation(i); |
+ ASSERT(animationData->isValidAnimation()); |
+ AtomicString animationName(animationData->name()); |
+ |
+ if (cssAnimations) { |
+ AnimationMap::const_iterator existing(cssAnimations->m_animations.find(animationName.impl())); |
+ if (existing != cssAnimations->m_animations.end()) { |
+ inactive.remove(animationName.impl()); |
+ continue; |
+ } |
+ } |
+ |
+ // If there's a delay, no styles will apply yet. |
+ if (animationData->isDelaySet() && animationData->delay()) |
+ continue; |
+ |
+ const StylePropertySet* keyframeStyles = firstKeyframeStyles(element, animationName.impl(), resolver); |
+ if (keyframeStyles) { |
+ if (!update) |
+ update = adoptPtr(new CSSAnimationUpdate()); |
+ update->addStyles(keyframeStyles); |
+ } |
+ } |
+ } |
+ |
+ if (!inactive.isEmpty() && !update) |
+ update = adoptPtr(new CSSAnimationUpdate()); |
+ for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != inactive.end(); ++iter) |
+ update->cancel(cssAnimations->m_animations.get(*iter)); |
+ |
+ return update.release(); |
+} |
+ |
+void CSSAnimations::update(Element* element, const RenderStyle* style) |
+{ |
+ const CSSAnimationDataList* animationDataList = style->animations(); |
+ HashSet<StringImpl*> inactive; |
+ for (AnimationMap::const_iterator iter = m_animations.begin(); iter != m_animations.end(); ++iter) |
+ inactive.add(iter->key); |
+ |
+ if (style->display() != NONE) { |
+ for (int i = 0; animationDataList && i < animationDataList->size(); ++i) { |
+ const CSSAnimationData* animationData = animationDataList->animation(i); |
+ ASSERT(animationData->isValidAnimation()); |
+ AtomicString animationName(animationData->name()); |
+ |
+ AnimationMap::const_iterator existing(m_animations.find(animationName.impl())); |
+ if (existing != m_animations.end()) { |
+ bool paused = animationData->playState() == AnimPlayStatePaused; |
+ existing->value->setPaused(paused); |
+ inactive.remove(animationName.impl()); |
+ continue; |
+ } |
+ |
+ KeyframeAnimationEffect::KeyframeVector keyframes; |
+ resolveKeyframes(element, animationName.impl(), element->document()->styleResolver(), keyframes); |
+ if (!keyframes.isEmpty()) { |
+ Timing timing; |
+ timingFromAnimationData(animationData, timing); |
+ m_animations.set(animationName.impl(), element->document()->timeline()->play( |
+ Animation::create(element, KeyframeAnimationEffect::create(keyframes), timing).get()).get()); |
+ } |
+ } |
+ } |
+ |
+ for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != inactive.end(); ++iter) |
+ m_animations.take(*iter)->cancel(); |
+} |
+ |
+void CSSAnimations::cancel() |
+{ |
+ for (AnimationMap::iterator iter = m_animations.begin(); iter != m_animations.end(); ++iter) |
+ iter->value->cancel(); |
+ |
+ m_animations.clear(); |
+} |
+ |
+} // namespace WebCore |