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 #ifndef TimedItemCalculations_h 32 #define TimedItemCalculations_h 33 34 #include "core/animation/TimedItem.h" 35 #include "core/animation/Timing.h" 36 #include "core/platform/animation/AnimationUtilities.h" 37 #include "wtf/MathExtras.h" 38 39 namespace WebCore { 40 41 static inline TimedItem::Phase calculatePhase(double activeDuration, double localTime, const Timing& specified) 42 { 43 ASSERT(activeDuration >= 0); 44 if (isNull(localTime)) 45 return TimedItem::PhaseNone; 46 if (localTime < specified.startDelay) 47 return TimedItem::PhaseBefore; 48 if (localTime >= specified.startDelay + activeDuration) 49 return TimedItem::PhaseAfter; 50 return TimedItem::PhaseActive; 51 } 52 53 static inline bool isActiveInParentPhase(TimedItem::Phase parentPhase, Timing::FillMode fillMode) 54 { 55 switch (parentPhase) { 56 case TimedItem::PhaseBefore: 57 return fillMode == Timing::FillModeBackwards || fillMode == Timing::FillModeBoth; 58 case TimedItem::PhaseActive: 59 return true; 60 case TimedItem::PhaseAfter: 61 return fillMode == Timing::FillModeForwards || fillMode == Timing::FillModeBoth; 62 default: 63 ASSERT_NOT_REACHED(); 64 return false; 65 } 66 } 67 68 static inline double calculateActiveTime(double activeDuration, double localTime, TimedItem::Phase parentPhase, TimedItem::Phase phase, const Timing& specified) 69 { 70 ASSERT(activeDuration >= 0); 71 ASSERT(phase == calculatePhase(activeDuration, localTime, specified)); 72 73 switch (phase) { 74 case TimedItem::PhaseBefore: 75 if (specified.fillMode == Timing::FillModeBackwards || specified.fillMode == Timing::FillModeBoth) 76 return 0; 77 return nullValue(); 78 case TimedItem::PhaseActive: 79 if (isActiveInParentPhase(parentPhase, specified.fillMode)) 80 return localTime - specified.startDelay; 81 return nullValue(); 82 case TimedItem::PhaseAfter: 83 if (specified.fillMode == Timing::FillModeForwards || specified.fillMode == Timing::FillModeBoth) 84 return activeDuration; 85 return nullValue(); 86 case TimedItem::PhaseNone: 87 ASSERT(isNull(localTime)); 88 return nullValue(); 89 default: 90 ASSERT_NOT_REACHED(); 91 return nullValue(); 92 } 93 } 94 95 static inline double calculateScaledActiveTime(double activeDuration, double activeTime, double startOffset, const Timing& specified) 96 { 97 ASSERT(activeDuration >= 0); 98 ASSERT(startOffset >= 0); 99 100 if (isNull(activeTime)) 101 return nullValue(); 102 103 ASSERT(activeTime >= 0); 104 105 return (specified.playbackRate < 0 ? activeTime - activeDuration : activeTime) * specified.playbackRate + startOffset; 106 } 107 108 static inline bool endsOnIterationBoundary(double iterationCount, double iterationStart) 109 { 110 return !fmod(iterationCount + iterationStart, 1); 111 } 112 113 static inline double calculateIterationTime(double iterationDuration, double repeatedDuration, double scaledActiveTime, double startOffset, const Timing& specified) 114 { 115 ASSERT(iterationDuration >= 0); 116 ASSERT(repeatedDuration == iterationDuration * specified.iterationCount); 117 118 if (isNull(scaledActiveTime)) 119 return nullValue(); 120 121 ASSERT(scaledActiveTime >= 0); 122 ASSERT(scaledActiveTime <= repeatedDuration + startOffset); 123 124 if (!iterationDuration) 125 return 0; 126 127 if (scaledActiveTime - startOffset == repeatedDuration && specified.iterationCount && endsOnIterationBoundary(specified.iterationCount, specified.iterationStart)) 128 return iterationDuration; 129 130 return fmod(scaledActiveTime, iterationDuration); 131 } 132 133 static inline double calculateCurrentIteration(double iterationDuration, double iterationTime, double scaledActiveTime, const Timing& specified) 134 { 135 ASSERT(iterationDuration >= 0); 136 ASSERT(isNull(iterationTime) || iterationTime >= 0); 137 138 if (isNull(scaledActiveTime)) 139 return nullValue(); 140 141 ASSERT(iterationTime >= 0); 142 ASSERT(iterationTime <= iterationDuration); 143 ASSERT(scaledActiveTime >= 0); 144 145 if (!scaledActiveTime) 146 return 0; 147 148 if (!iterationDuration) 149 return floor(specified.iterationStart + specified.iterationCount); 150 151 if (iterationTime == iterationDuration) 152 return specified.iterationStart + specified.iterationCount - 1; 153 154 return floor(scaledActiveTime / iterationDuration); 155 } 156 157 static inline double calculateDirectedTime(double currentIteration, double iterationDuration, double iterationTime, const Timing& specified) 158 { 159 ASSERT(isNull(currentIteration) || currentIteration >= 0); 160 ASSERT(iterationDuration > 0); 161 162 if (isNull(iterationTime)) 163 return nullValue(); 164 165 ASSERT(currentIteration >= 0); 166 ASSERT(iterationTime >= 0); 167 ASSERT(iterationTime <= iterationDuration); 168 169 const bool currentIterationIsOdd = fmod(currentIteration, 2) >= 1; 170 const bool currentDirectionIsForwards = specified.direction == Timing::PlaybackDirectionNormal 171 || (specified.direction == Timing::PlaybackDirectionAlternate && !currentIterationIsOdd) 172 || (specified.direction == Timing::PlaybackDirectionAlternateReverse && currentIterationIsOdd); 173 174 return currentDirectionIsForwards ? iterationTime : iterationDuration - iterationTime; 175 } 176 177 static inline double calculateTransformedTime(double currentIteration, double iterationDuration, double iterationTime, const Timing& specified) 178 { 179 ASSERT(isNull(currentIteration) || currentIteration >= 0); 180 ASSERT(iterationDuration > 0); 181 ASSERT(isNull(iterationTime) || (iterationTime >= 0 && iterationTime <= iterationDuration)); 182 183 double directedTime = calculateDirectedTime(currentIteration, iterationDuration, iterationTime, specified); 184 if (isNull(directedTime)) 185 return nullValue(); 186 return specified.timingFunction ? 187 iterationDuration * specified.timingFunction->evaluate(directedTime / iterationDuration, accuracyForDuration(iterationDuration)) : 188 directedTime; 189 } 190 191 } // namespace WebCore 192 193 #endif 194