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/TimedItem.h"
33#include "core/animation/TimedItemCalculations.h"
34
35namespace WebCore {
36
37TimedItem::TimedItem(const Timing& timing, PassOwnPtr<TimedItemEventDelegate> eventDelegate)
38    : m_parent(0)
39    , m_player(0)
40    , m_startTime(0)
41    , m_specified(timing)
42    , m_calculated()
43    , m_eventDelegate(eventDelegate)
44{
45    timing.assertValid();
46}
47
48void TimedItem::updateInheritedTime(double inheritedTime) const
49{
50    const double localTime = inheritedTime - m_startTime;
51    const double iterationDuration = m_specified.hasIterationDuration
52        ? m_specified.iterationDuration
53        : intrinsicIterationDuration();
54
55    const double repeatedDuration = iterationDuration * m_specified.iterationCount;
56    const double activeDuration = m_specified.playbackRate
57        ? repeatedDuration / abs(m_specified.playbackRate)
58        : std::numeric_limits<double>::infinity();
59
60    const TimedItem::Phase phase = calculatePhase(activeDuration, localTime, m_specified);
61    // FIXME: parentPhase depends on groups being implemented.
62    const TimedItem::Phase parentPhase = TimedItem::PhaseActive;
63    const double activeTime = calculateActiveTime(activeDuration, localTime, parentPhase, phase, m_specified);
64
65    double currentIteration = nullValue();
66    double timeFraction = nullValue();
67    ASSERT(iterationDuration >= 0);
68    if (iterationDuration) {
69        const double startOffset = m_specified.iterationStart * iterationDuration;
70        const double scaledActiveTime = calculateScaledActiveTime(activeDuration, activeTime, startOffset, m_specified);
71        const double iterationTime = calculateIterationTime(iterationDuration, repeatedDuration, scaledActiveTime, startOffset, m_specified);
72
73        currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
74        timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified) / iterationDuration;
75    } else {
76        const double iterationDuration = 1;
77        const double repeatedDuration = iterationDuration * m_specified.iterationCount;
78        const double activeDuration = m_specified.playbackRate ? repeatedDuration / abs(m_specified.playbackRate) : std::numeric_limits<double>::infinity();
79        const double newLocalTime = localTime < m_specified.startDelay ? m_specified.startDelay - 1 : activeDuration + m_specified.startDelay;
80        const TimedItem::Phase phase = calculatePhase(activeDuration, newLocalTime, m_specified);
81        const double activeTime = calculateActiveTime(activeDuration, newLocalTime, parentPhase, phase, m_specified);
82        const double startOffset = m_specified.iterationStart * iterationDuration;
83        const double scaledActiveTime = calculateScaledActiveTime(activeDuration, activeTime, startOffset, m_specified);
84        const double iterationTime = calculateIterationTime(iterationDuration, repeatedDuration, scaledActiveTime, startOffset, m_specified);
85
86        currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
87        timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified);
88    }
89
90    const double lastIteration = m_calculated.currentIteration;
91    m_calculated.currentIteration = currentIteration;
92    m_calculated.activeDuration = activeDuration;
93    m_calculated.timeFraction = timeFraction;
94
95    const bool wasInEffect = m_calculated.isInEffect;
96    const bool wasInPlay = m_calculated.isInPlay;
97    m_calculated.isInEffect = !isNull(activeTime);
98    m_calculated.isInPlay = phase == PhaseActive && (!m_parent || m_parent->isInPlay());
99    m_calculated.isCurrent = phase == PhaseBefore || isInPlay() || (m_parent && m_parent->isCurrent());
100
101    if (m_eventDelegate && (isInPlay() != wasInPlay || (isInPlay() && lastIteration != currentIteration)))
102        m_eventDelegate->onEventCondition(wasInPlay, isInPlay(), lastIteration, currentIteration);
103
104    // FIXME: This probably shouldn't be recursive.
105    updateChildrenAndEffects(wasInEffect);
106}
107
108TimedItem::CalculatedTiming::CalculatedTiming()
109    : activeDuration(nullValue())
110    , currentIteration(nullValue())
111    , timeFraction(nullValue())
112    , isCurrent(false)
113    , isInEffect(false)
114    , isInPlay(false)
115{
116}
117
118} // namespace WebCore
119