2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "core/animation/Player.h"
35 #include "core/animation/Animation.h"
36 #include "core/animation/DocumentTimeline.h"
42 double effectiveTime(double time) { return isNull(time) ? 0 : time; }
46 PassRefPtr<Player> Player::create(DocumentTimeline& timeline, TimedItem* content)
48 return adoptRef(new Player(timeline, content));
51 Player::Player(DocumentTimeline& timeline, TimedItem* content)
52 : m_pauseStartTime(nullValue())
55 , m_startTime(nullValue())
57 , m_timeline(timeline)
58 , m_isPausedForTesting(false)
61 m_content->attach(this);
70 void Player::setStartTime(double startTime)
72 ASSERT(!isNull(startTime));
73 ASSERT(!hasStartTime());
74 m_startTime = startTime;
78 double Player::currentTimeBeforeDrift() const
80 if (isNull(m_startTime))
82 return (effectiveTime(m_timeline.currentTime()) - startTime()) * m_playbackRate;
85 bool Player::maybeStartAnimationOnCompositor()
87 // FIXME: Support starting compositor animations that have a fixed
89 ASSERT(!hasStartTime());
90 if (!m_content || !m_content->isAnimation())
93 return toAnimation(m_content.get())->maybeStartAnimationOnCompositor();
96 bool Player::hasActiveAnimationsOnCompositor()
98 if (!m_content || !m_content->isAnimation())
100 return toAnimation(m_content.get())->hasActiveAnimationsOnCompositor();
103 void Player::cancelAnimationOnCompositor()
105 if (hasActiveAnimationsOnCompositor())
106 toAnimation(m_content.get())->cancelAnimationOnCompositor();
109 double Player::pausedTimeDrift() const
111 ASSERT(pausedInternal());
112 return currentTimeBeforeDrift() - m_pauseStartTime;
115 double Player::timeDrift() const
117 return pausedInternal() ? pausedTimeDrift() : m_timeDrift;
120 double Player::currentTime() const
122 return currentTimeBeforeDrift() - timeDrift();
125 bool Player::update(double* timeToEffectChange, bool* didTriggerStyleRecalc)
128 if (timeToEffectChange)
129 *timeToEffectChange = std::numeric_limits<double>::infinity();
130 if (didTriggerStyleRecalc)
131 *didTriggerStyleRecalc = false;
135 double inheritedTime = isNull(m_timeline.currentTime()) ? nullValue() : currentTime();
136 bool didTriggerStyleRecalcLocal = m_content->updateInheritedTime(inheritedTime);
138 if (timeToEffectChange)
139 *timeToEffectChange = m_content->timeToEffectChange();
140 if (didTriggerStyleRecalc)
141 *didTriggerStyleRecalc = didTriggerStyleRecalcLocal;
142 return m_content->isCurrent() || m_content->isInEffect();
145 void Player::cancel()
150 ASSERT(m_content->player() == this);
155 void Player::setCurrentTime(double seekTime)
157 if (pausedInternal())
158 m_pauseStartTime = seekTime;
160 m_timeDrift = currentTimeBeforeDrift() - seekTime;
162 if (m_isPausedForTesting && hasActiveAnimationsOnCompositor())
163 toAnimation(m_content.get())->pauseAnimationForTestingOnCompositor(currentTime());
167 void Player::pauseForTesting()
169 RELEASE_ASSERT(!paused());
170 if (!m_isPausedForTesting && hasActiveAnimationsOnCompositor())
171 toAnimation(m_content.get())->pauseAnimationForTestingOnCompositor(currentTime());
172 m_isPausedForTesting = true;
176 void Player::setPaused(bool newValue)
178 ASSERT(!m_isPausedForTesting);
179 setPausedImpl(newValue);
182 void Player::setPausedImpl(bool newValue)
184 if (pausedInternal() == newValue)
188 // FIXME: resume compositor animation rather than pull back to main-thread
189 cancelAnimationOnCompositor();
190 m_pauseStartTime = currentTime();
192 m_timeDrift = pausedTimeDrift();
193 m_pauseStartTime = nullValue();
197 void Player::setPlaybackRate(double newRate)
199 double previousTime = currentTime();
200 m_playbackRate = newRate;
201 setCurrentTime(previousTime);