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 Player* DocumentTimeline::createPlayer(TimedItem* child)
66 RefPtr<Player> player = Player::create(*this, child);
67 Player* result = player.get();
68 m_players.append(player.release());
69 if (m_document->view())
70 m_timing->serviceOnNextFrame();
74 Player* DocumentTimeline::play(TimedItem* child)
76 Player* player = createPlayer(child);
77 player->setStartTime(currentTime());
81 void DocumentTimeline::wake()
83 m_timing->serviceOnNextFrame();
86 bool DocumentTimeline::serviceAnimations()
88 TRACE_EVENT0("webkit", "DocumentTimeline::serviceAnimations");
90 m_timing->cancelWake();
92 double timeToNextEffect = std::numeric_limits<double>::infinity();
93 bool didTriggerStyleRecalc = false;
94 for (int i = m_players.size() - 1; i >= 0; --i) {
95 double playerNextEffect;
96 bool playerDidTriggerStyleRecalc;
97 if (!m_players[i]->update(&playerNextEffect, &playerDidTriggerStyleRecalc))
99 didTriggerStyleRecalc |= playerDidTriggerStyleRecalc;
100 if (playerNextEffect < timeToNextEffect)
101 timeToNextEffect = playerNextEffect;
104 if (!m_players.isEmpty()) {
105 if (timeToNextEffect < s_minimumDelay)
106 m_timing->serviceOnNextFrame();
107 else if (timeToNextEffect != std::numeric_limits<double>::infinity())
108 m_timing->wakeAfter(timeToNextEffect - s_minimumDelay);
111 return didTriggerStyleRecalc;
114 void DocumentTimeline::setZeroTime(double zeroTime)
116 ASSERT(isNull(m_zeroTime));
117 m_zeroTime = zeroTime;
118 ASSERT(!isNull(m_zeroTime));
121 void DocumentTimeline::DocumentTimelineTiming::wakeAfter(double duration)
123 m_timer.startOneShot(duration);
126 void DocumentTimeline::DocumentTimelineTiming::cancelWake()
131 void DocumentTimeline::DocumentTimelineTiming::serviceOnNextFrame()
133 if (m_timeline->m_document->view())
134 m_timeline->m_document->view()->scheduleAnimation();
137 double DocumentTimeline::currentTime()
139 return m_document->animationClock().currentTime() - m_zeroTime;
142 void DocumentTimeline::pauseAnimationsForTesting(double pauseTime)
144 for (size_t i = 0; i < m_players.size(); i++) {
145 m_players[i]->pauseForTesting();
146 m_players[i]->setCurrentTime(pauseTime);
150 void DocumentTimeline::dispatchEvents()
152 Vector<EventToDispatch> events = m_events;
154 for (size_t i = 0; i < events.size(); i++)
155 events[i].target->dispatchEvent(events[i].event.release());
158 void DocumentTimeline::dispatchEventsAsync()
160 if (m_events.isEmpty() || m_eventDistpachTimer.isActive())
162 m_eventDistpachTimer.startOneShot(0);
165 void DocumentTimeline::eventDispatchTimerFired(Timer<DocumentTimeline>*)
170 size_t DocumentTimeline::numberOfActiveAnimationsForTesting() const
172 if (isNull(m_zeroTime))
174 // Includes all players whose directly associated timed items
175 // are current or in effect.
176 if (isNull(m_zeroTime))
179 for (size_t i = 0; i < m_players.size(); ++i) {
180 const TimedItem* timedItem = m_players[i]->source();
181 if (m_players[i]->hasStartTime())
182 count += (timedItem && (timedItem->isCurrent() || timedItem->isInEffect()));