Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2013 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "core/animation/css/CSSAnimations.h" | |
| 33 | |
| 34 #include "core/animation/DocumentTimeline.h" | |
| 35 #include "core/animation/KeyframeAnimationEffect.h" | |
| 36 #include "core/css/CSSKeyframeRule.h" | |
| 37 #include "core/css/resolver/StyleResolver.h" | |
| 38 #include "core/dom/AnimationEvent.h" | |
| 39 #include "core/dom/Element.h" | |
| 40 #include "core/dom/EventNames.h" | |
| 41 #include "core/platform/animation/CSSAnimationDataList.h" | |
| 42 #include "core/platform/animation/TimingFunction.h" | |
| 43 #include "wtf/HashSet.h" | |
| 44 | |
| 45 namespace WebCore { | |
| 46 | |
| 47 void timingFromAnimationData(const CSSAnimationData* animationData, Timing& timi ng) | |
| 48 { | |
| 49 if (animationData->isDelaySet()) | |
| 50 timing.startDelay = animationData->delay(); | |
| 51 if (animationData->isDurationSet()) { | |
| 52 timing.iterationDuration = animationData->duration(); | |
| 53 timing.hasIterationDuration = true; | |
| 54 } | |
| 55 if (animationData->isIterationCountSet()) { | |
| 56 if (animationData->iterationCount() == CSSAnimationData::IterationCountI nfinite) | |
| 57 timing.iterationCount = std::numeric_limits<double>::infinity(); | |
| 58 else | |
| 59 timing.iterationCount = animationData->iterationCount(); | |
| 60 } | |
| 61 if (animationData->isTimingFunctionSet()) { | |
| 62 if (!animationData->timingFunction()->isLinearTimingFunction()) | |
| 63 timing.timingFunction = animationData->timingFunction(); | |
| 64 } else { | |
| 65 // CSS default is ease, default in model is linear. | |
| 66 timing.timingFunction = CubicBezierTimingFunction::preset(CubicBezierTim ingFunction::Ease); | |
| 67 } | |
| 68 if (animationData->isFillModeSet()) { | |
| 69 switch (animationData->fillMode()) { | |
| 70 case AnimationFillModeForwards: | |
| 71 timing.fillMode = Timing::FillModeForwards; | |
| 72 break; | |
| 73 case AnimationFillModeBackwards: | |
| 74 timing.fillMode = Timing::FillModeBackwards; | |
| 75 break; | |
| 76 case AnimationFillModeBoth: | |
| 77 timing.fillMode = Timing::FillModeBoth; | |
| 78 break; | |
| 79 case AnimationFillModeNone: | |
| 80 timing.fillMode = Timing::FillModeNone; | |
| 81 break; | |
| 82 default: | |
| 83 ASSERT_NOT_REACHED(); | |
| 84 } | |
| 85 } | |
| 86 if (animationData->isDirectionSet()) { | |
| 87 switch (animationData->direction()) { | |
| 88 case CSSAnimationData::AnimationDirectionNormal: | |
| 89 timing.direction = Timing::PlaybackDirectionNormal; | |
| 90 break; | |
| 91 case CSSAnimationData::AnimationDirectionAlternate: | |
| 92 timing.direction = Timing::PlaybackDirectionAlternate; | |
| 93 break; | |
| 94 case CSSAnimationData::AnimationDirectionReverse: | |
| 95 timing.direction = Timing::PlaybackDirectionReverse; | |
| 96 break; | |
| 97 case CSSAnimationData::AnimationDirectionAlternateReverse: | |
| 98 timing.direction = Timing::PlaybackDirectionAlternateReverse; | |
| 99 break; | |
| 100 default: | |
| 101 ASSERT_NOT_REACHED(); | |
| 102 } | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 bool CSSAnimations::needsUpdate(const Element* element, const RenderStyle* style ) | |
| 107 { | |
| 108 ActiveAnimations* activeAnimations = element->activeAnimations(); | |
| 109 const CSSAnimationDataList* animations = style->animations(); | |
| 110 const CSSAnimations* cssAnimations = activeAnimations ? activeAnimations->cs sAnimations() : 0; | |
| 111 EDisplay display = style->display(); | |
| 112 return (display != NONE && animations && animations->size()) || (cssAnimatio ns && !cssAnimations->isEmpty()); | |
| 113 } | |
| 114 | |
| 115 PassOwnPtr<CSSAnimationUpdate> CSSAnimations::calculateUpdate(const Element* ele ment, EDisplay display, const CSSAnimations* cssAnimations, const CSSAnimationDa taList* animationDataList, StyleResolver* resolver) | |
| 116 { | |
| 117 OwnPtr<CSSAnimationUpdate> update; | |
| 118 HashSet<StringImpl*> inactive; | |
| 119 if (cssAnimations) | |
| 120 for (AnimationMap::const_iterator iter = cssAnimations->m_animations.beg in(); iter != cssAnimations->m_animations.end(); ++iter) | |
| 121 inactive.add(iter->key); | |
| 122 | |
| 123 RefPtr<MutableStylePropertySet> newStyles; | |
| 124 if (display != NONE) { | |
| 125 for (size_t i = 0; animationDataList && i < animationDataList->size(); + +i) { | |
| 126 const CSSAnimationData* animationData = animationDataList->animation (i); | |
| 127 ASSERT(animationData->isValidAnimation()); | |
| 128 AtomicString animationName(animationData->name()); | |
| 129 | |
| 130 if (cssAnimations) { | |
| 131 AnimationMap::const_iterator existing(cssAnimations->m_animation s.find(animationName.impl())); | |
| 132 if (existing != cssAnimations->m_animations.end()) { | |
| 133 inactive.remove(animationName.impl()); | |
| 134 continue; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 // If there's a delay, no styles will apply yet. | |
| 139 if (animationData->isDelaySet() && animationData->delay()) | |
|
Steve Block
2013/08/06 03:57:01
Do we check/force anywhere that delay() is non-neg
dstockwell
2013/08/06 05:43:25
Good catch, this can be negative and will be hard
Mike Lawther (Google)
2013/08/06 06:01:11
It sounds like we'll want to deal with negative ev
| |
| 140 continue; | |
| 141 | |
| 142 const StylePropertySet* keyframeStyles = resolver->firstKeyframeStyl es(element, animationName.impl()); | |
| 143 if (keyframeStyles) { | |
| 144 if (!update) | |
| 145 update = adoptPtr(new CSSAnimationUpdate()); | |
| 146 update->addStyles(keyframeStyles); | |
| 147 } | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 if (!inactive.isEmpty() && !update) | |
| 152 update = adoptPtr(new CSSAnimationUpdate()); | |
| 153 for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != i nactive.end(); ++iter) | |
| 154 update->cancel(cssAnimations->m_animations.get(*iter)); | |
| 155 | |
| 156 return update.release(); | |
| 157 } | |
| 158 | |
| 159 void CSSAnimations::update(Element* element, const RenderStyle* style) | |
| 160 { | |
| 161 const CSSAnimationDataList* animationDataList = style->animations(); | |
| 162 HashSet<StringImpl*> inactive; | |
| 163 for (AnimationMap::const_iterator iter = m_animations.begin(); iter != m_ani mations.end(); ++iter) | |
| 164 inactive.add(iter->key); | |
| 165 | |
| 166 if (style->display() != NONE) { | |
| 167 for (size_t i = 0; animationDataList && i < animationDataList->size(); + +i) { | |
| 168 const CSSAnimationData* animationData = animationDataList->animation (i); | |
| 169 ASSERT(animationData->isValidAnimation()); | |
| 170 AtomicString animationName(animationData->name()); | |
| 171 | |
| 172 AnimationMap::const_iterator existing(m_animations.find(animationNam e.impl())); | |
| 173 if (existing != m_animations.end()) { | |
| 174 bool paused = animationData->playState() == AnimPlayStatePaused; | |
| 175 existing->value->setPaused(paused); | |
| 176 inactive.remove(animationName.impl()); | |
| 177 continue; | |
| 178 } | |
| 179 | |
| 180 KeyframeAnimationEffect::KeyframeVector keyframes; | |
| 181 element->document()->styleResolver()->resolveKeyframes(element, anim ationName.impl(), keyframes); | |
| 182 if (!keyframes.isEmpty()) { | |
| 183 Timing timing; | |
| 184 timingFromAnimationData(animationData, timing); | |
| 185 m_animations.set(animationName.impl(), element->document()->time line()->play( | |
| 186 Animation::create(element, KeyframeAnimationEffect::create(k eyframes), timing).get()).get()); | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != i nactive.end(); ++iter) | |
| 192 m_animations.take(*iter)->cancel(); | |
| 193 } | |
| 194 | |
| 195 void CSSAnimations::cancel() | |
| 196 { | |
| 197 for (AnimationMap::iterator iter = m_animations.begin(); iter != m_animation s.end(); ++iter) | |
| 198 iter->value->cancel(); | |
| 199 | |
| 200 m_animations.clear(); | |
| 201 } | |
| 202 | |
| 203 } // namespace WebCore | |
| OLD | NEW |