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.
32 #include "core/animation/DocumentTimeline.h"
34 #include "core/animation/ActiveAnimations.h"
35 #include "core/animation/AnimationClock.h"
36 #include "core/dom/Document.h"
37 #include "core/frame/FrameView.h"
41 // This value represents 1 frame at 30Hz plus a little bit of wiggle room.
42 // TODO: Plumb a nominal framerate through and derive this value from that.
43 const double DocumentTimeline::s_minimumDelay = 0.04;
46 PassRefPtr<DocumentTimeline> DocumentTimeline::create(Document* document, PassOwnPtr<PlatformTiming> timing)
48 return adoptRef(new DocumentTimeline(document, timing));
51 DocumentTimeline::DocumentTimeline(Document* document, PassOwnPtr<PlatformTiming> timing)
52 : m_zeroTime(nullValue())
53 , m_document(document)
54 , m_eventDistpachTimer(this, &DocumentTimeline::eventDispatchTimerFired)
57 m_timing = adoptPtr(new DocumentTimelineTiming(this));
64 DocumentTimeline::~DocumentTimeline()
66 for (HashSet<Player*>::iterator it = m_players.begin(); it != m_players.end(); ++it)
67 (*it)->timelineDestroyed();
70 Player* DocumentTimeline::createPlayer(TimedItem* child)
72 RefPtr<Player> player = Player::create(*this, child);
73 Player* result = player.get();
74 m_players.add(result);
75 m_currentPlayers.append(player.release());
76 setHasPlayerNeedingUpdate();
80 Player* DocumentTimeline::play(TimedItem* child)
82 Player* player = createPlayer(child);
83 player->setStartTime(currentTime());
87 void DocumentTimeline::wake()
89 m_timing->serviceOnNextFrame();
92 bool DocumentTimeline::serviceAnimations()
94 TRACE_EVENT0("webkit", "DocumentTimeline::serviceAnimations");
96 m_timing->cancelWake();
98 double timeToNextEffect = std::numeric_limits<double>::infinity();
99 bool didTriggerStyleRecalc = false;
100 for (int i = m_currentPlayers.size() - 1; i >= 0; --i) {
101 RefPtr<Player> player = m_currentPlayers[i].get();
102 bool playerDidTriggerStyleRecalc;
103 if (!player->update(&playerDidTriggerStyleRecalc))
104 m_currentPlayers.remove(i);
105 timeToNextEffect = std::min(timeToNextEffect, player->timeToEffectChange());
106 didTriggerStyleRecalc |= playerDidTriggerStyleRecalc;
109 if (!m_currentPlayers.isEmpty()) {
110 if (timeToNextEffect < s_minimumDelay)
111 m_timing->serviceOnNextFrame();
112 else if (timeToNextEffect != std::numeric_limits<double>::infinity())
113 m_timing->wakeAfter(timeToNextEffect - s_minimumDelay);
116 m_hasPlayerNeedingUpdate = false;
117 return didTriggerStyleRecalc;
120 void DocumentTimeline::setZeroTime(double zeroTime)
122 ASSERT(isNull(m_zeroTime));
123 m_zeroTime = zeroTime;
124 ASSERT(!isNull(m_zeroTime));
128 void DocumentTimeline::DocumentTimelineTiming::wakeAfter(double duration)
130 m_timer.startOneShot(duration);
133 void DocumentTimeline::DocumentTimelineTiming::cancelWake()
138 void DocumentTimeline::DocumentTimelineTiming::serviceOnNextFrame()
140 if (m_timeline->m_document && m_timeline->m_document->view())
141 m_timeline->m_document->view()->scheduleAnimation();
144 double DocumentTimeline::currentTime()
147 return std::numeric_limits<double>::quiet_NaN();
148 return m_document->animationClock().currentTime() - m_zeroTime;
151 void DocumentTimeline::pauseAnimationsForTesting(double pauseTime)
153 for (size_t i = 0; i < m_currentPlayers.size(); i++)
154 m_currentPlayers[i]->pauseForTesting(pauseTime);
158 void DocumentTimeline::setHasPlayerNeedingUpdate()
160 m_hasPlayerNeedingUpdate = true;
161 if (m_document && m_document->view() && !m_document->view()->isServicingAnimations())
162 m_timing->serviceOnNextFrame();
165 void DocumentTimeline::dispatchEvents()
167 Vector<EventToDispatch> events = m_events;
169 for (size_t i = 0; i < events.size(); i++)
170 events[i].target->dispatchEvent(events[i].event.release());
173 void DocumentTimeline::dispatchEventsAsync()
175 if (m_events.isEmpty() || m_eventDistpachTimer.isActive())
177 m_eventDistpachTimer.startOneShot(0);
180 void DocumentTimeline::eventDispatchTimerFired(Timer<DocumentTimeline>*)
185 size_t DocumentTimeline::numberOfActiveAnimationsForTesting() const
187 if (isNull(m_zeroTime))
189 // Includes all players whose directly associated timed items
190 // are current or in effect.
191 if (isNull(m_zeroTime))
194 for (size_t i = 0; i < m_currentPlayers.size(); ++i) {
195 const TimedItem* timedItem = m_currentPlayers[i]->source();
196 if (m_currentPlayers[i]->hasStartTime())
197 count += (timedItem && (timedItem->isCurrent() || timedItem->isInEffect()));
202 void DocumentTimeline::detachFromDocument() {